Maciej Dziuban
Maciej Dziuban

Reputation: 502

Android Adapter's convertview not updated

I have some weird events occurring in getView() method in custom adapter for listview. It seems as if cached views are only updated during app's end. I've read a bunch of tutorials, but never seen an issue like this.

When I add first item to an adapter, everything is fine - convertView is null, I inflate view and return it. However, when I try adding second item, getView() gets sent exactly the same convertView, for position 0 and 1 (even though for position 1 it should be null), resulting in identical items.

After restarting app, every added item is viewed properly (data is serialized), but trying to add more items still results in views identical to the first one.

I'm sure the problem lays in adapter, getView(), because when I stop using convertView and just inflate items every single time, it works perfectly fine.

This is how my methods look like. mNames is Vector; when it changes, it's serialized and the adapter is notified. This vector sets adapter count (see getCount). It's deserialized in onCreate(), right before being passed to adapter.

@Override
    public int getCount() {
        return mNames.size();
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = layoutInflater.inflate(R.layout.grouplist_listview_item, null);

        //just saying, there are more subviews; I didn't find it relevant
        ((TextView)convertView.findViewById(R.id.grouplist_listview_item_text)).setText(mNames.elementAt(position));
    }
    return convertView;
}

So I don't really understand two things here:

Upvotes: 1

Views: 517

Answers (1)

The Original Android
The Original Android

Reputation: 6215

I think you need to read a good tutorial. I know good webpage @ Using an ArrayAdapter with ListView. Search text for "Defining the Adapter" to see the getView() code.

Code snippet:

    @Override
    public View getView(int position, @Nullable View convertView, ViewGroup parent) {
       // Get the data item for this position
       User user = getItem(position);    
       // Check if an existing view is being reused, otherwise inflate the view
       if (convertView == null) {
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
       }
       // Lookup view for data population
       TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
       // Populate the data into the template view using the data object
       tvName.setText(user.name);
       // Return the completed view to render on screen
       return convertView;
    }

Note: The code uses setText method. This will test your code with the ArrayAdapter and your layout.

Upvotes: 0

Related Questions