Android的:如何刷新ListView的内容是什么? [英] Android: how to refresh ListView contents?
问题描述
我的ListView使用BaseAdapter的延伸,我不能让它正确地刷新。当我刷新,看来旧数据借鉴了新数据的顶部,直到一个滚动事件发生的情况。旧的排借鉴的新行前,但是当我开始滚动旧行消失。
我已经打过电话invalidateViews(),notifyDataSetChanged(),和notifyDataSetInvalidated()。我的code看起来是这样的:
私人无效了updateData()
{
名单< DataItems> newList = getNewList();
MyAdapter适配器=新MyAdapter(的getContext());
//我的适配器拥有DataItems的内部列表
adapter.setList(newList);
mList.setAdapter(适配器);
adapter.notifyDataSetChanged();
mList.invalidateViews();
}
对于那些仍然有问题,我解决了这种方式:
名单,其中,项目> newItems = databaseHandler.getItems();
ListArrayAdapter.clear();
ListArrayAdapter.addAll(newItems);
ListArrayAdapter.notifyDataSetChanged();
databaseHandler.close();
我先清除从适配器的数据,则添加的项目的新的集合,然后才设置 notifyDataSetChanged();
这是不明确的,我在第一,所以我想指出这一点。请留意,而无需调用 notifyDataSetChanged()
视图将不会被更新。
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.
这篇关于Android的:如何刷新ListView的内容是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!