Navigation Drawer Listview does not update new items even after calling notifyDataSetChange() on Adapter

StackOverflow https://stackoverflow.com/questions/22713972

Domanda

I have a ListView rendered on a Navigation Drawer. The content of the list view is provided by my custom adapter (derived from BaseAdapter) which appends two arrays one after another like below

*@Override
public int getCount() {
  return array1.size() + array2.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  if (position < array1.size()) 
     // return a view  generated from first array
  }
  else {
     // returns a view generated from the second array
  }
}

The ListView is connected to the adapter like this

mDrawerList.setAdapter(adapter);

Initially right after onCreate(), array2 is empty; only array1 is populated and rendered on the ListView correctly. Afterwards, after a slight delay, a background thread populates the array2 and I want the new items to show up on the list. To achieve that, I issue a notifyDataSetChanged() to the adapter from the main thread. Unfortunately, this does not update the ListView UI with the new items from the second array. While debugging this, I found out that my getCount() method is getting called which is correctly returning the combined array size count, but the getView() method is not getting called at all. I ensured that the ListView is not hidden by other overlapping widgets.

Strangely, once I tap on the ListView, it gets refreshed with the newly added items as desired. What do I have to do over and above calling notifyDataSetChanged() to trigger such a refresh programmatically?

È stato utile?

Soluzione

Best approach is to use ArrayAdapter . so when you have new item to add to the list.. just call adapter.add("your object"), which will take care of calling getView() for the item added.

Note : This will add the item at last item in list view.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top