Android:如何刷新 ListView 内容? [英] Android: how to refresh ListView contents?
问题描述
我的 ListView
正在使用 BaseAdapter
的扩展,我无法正确刷新它.当我刷新时,旧数据似乎在新数据之上绘制,直到发生滚动事件.旧行绘制在新行之上,但当我开始滚动时旧行消失.
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.
我尝试过调用 invalidateViews()
、notifyDataSetChanged()
和 notifyDataSetInvalidated()
.我的代码看起来像:
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();
我首先从适配器清除数据,然后添加新的项目集合,然后才设置 notifyDataSetChanged();
一开始我并不清楚这一点,所以我想指出这一点.请注意,如果不调用 notifyDataSetChanged()
,视图将不会更新.
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.
这篇关于Android:如何刷新 ListView 内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!