My ListView
is using an extension of BaseAdapter
, I can not get it to refresh properly. When I refresh, it appears that the old data draws on top of the new data, until a scroll event happens. The old rows draw on top of the new rows, but the old rows disappear when I start scrolling.
I have tried calling invalidateViews()
, notifyDataSetChanged()
, and notifyDataSetInvalidated()
. My code looks something like:
private void updateData()
{
List<DataItems> newList = getNewList();
MyAdapter adapter = new MyAdapter(getContext());
//my adapter holds an internal list of DataItems
adapter.setList(newList);
mList.setAdapter(adapter);
adapter.notifyDataSetChanged();
mList.invalidateViews();
}
To those still having problems, I solved it this way:
List<Item> newItems = databaseHandler.getItems();
ListArrayAdapter.clear();
ListArrayAdapter.addAll(newItems);
ListArrayAdapter.notifyDataSetChanged();
databaseHandler.close();
I first cleared the data from the adapter, then added the new collection of items, and only then set notifyDataSetChanged();
This was not clear for me at first, so I wanted to point this out. Take note that without calling notifyDataSetChanged()
the view won't be updated.
In my understanding, if you want to refresh ListView immediately when data has changed, you should call notifyDataSetChanged()
in RunOnUiThread()
.
private void updateData() {
List<Data> newData = getYourNewData();
mAdapter.setList(yourNewList);
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}