Question

I feel a bit stupid as i can't find the answer to this question, which makes me think i'm actually asking the wrong question. However, here goes...

I have a list view, and a listviewitem defined in xml, with a couple of fields, nothing special. All set to visible.

Then I bind to my ListView using a custom ArrayAdapter, and want to hide one of my text views, on row 5. However, it seems to be hiding my TextView on item 0 and item 5. Which is a bit odd? I've simplified the code, to reproduce the problem and hopefully someone will be able to help me...

My Adapter

public class MenuScreenAdapter extends ArrayAdapter<String>
{
    private List<String> _items;
    private Context _context;

    public MenuScreenAdapter(Context context, List<String> items)
    {
        super(context, R.layout.list_menu_item, items);

        _context = context;
        _items = items;
    }

    private MenuScreenAdapter(Context context, int textViewResourceId)
    {
        super(context, textViewResourceId); 
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;

        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_menu_item, null);
        }
        String o = _items.get(position);    
        if (o != null)
        {
            TextView tt = (TextView) v.findViewById(R.id.list_menu_item_name);
            if (tt != null)
                tt.setText(o);

            if (position == 5)
                tt.setVisibility(View.GONE);
        }
        return v;
    }
}

My Binding Code

    // Load everything up that we need
    List<String> items = new ArrayList<String>();
    items.add("One");
    items.add("Two");
    items.add("Three");
    items.add("Four");
    items.add("Five");
    items.add("Six");
    items.add("Seven");
    items.add("Eight");
    items.add("Nine");
    items.add("Ten");

    // Get the ListView, and set it's adapter. The HomeScreenAdapter
    // takes care of the rest
    ListView homeScreenListView = (ListView) _mainActivity.findViewById(R.id.view_home_list);
    homeScreenListView.setOnItemClickListener(ItemSelected);
    homeScreenListView.setAdapter(new MenuScreenAdapter(_mainActivity.getBaseContext(), items));

Thanks in advance!

Was it helpful?

Solution

Since row views are reused by ArrayAdapter, once the View.GONE is set, it will cary on to the next row, where this view will be reused. In your case, you set View.GONE to textview in the fifth row, moved list a little and arrayadapter decided to reuse your fifth row layout to display the first row, since no changes were done to it, the textView still remains hidden.

Just do the:

if (position == 5) {
            tt.setVisibility(View.GONE);
} else {
            tt.setVisibility(View.VISIBLE);
}

P.S. If you still haven't, watch a presentation about ListViews from google. Tons of usefult info there. ListViews

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top