notifyDataSetChanged例子 [英] notifyDataSetChanged example
问题描述
我试图在用我的 Android应用程序
的 notifyDataSetChanged()
方法的 ArrayAdapter
,但它不为我工作。
I'm trying to use in my Android Application
the notifyDataSetChanged()
method for an ArrayAdapter
but it doesn't work for me.
我发现的答案这里,即 notifyDataSetChanged()
应在主线程中运行,但没有例子的。
I found as answer here, that notifyDataSetChanged()
should run in the main thread, but there was no example for that.
可能有人发的一个例子,或者至少一个链接?!
Could anybody send an example or at least a link?!
推荐答案
对于 ArrayAdapter
, notifyDataSetChanged
只有作品如果您使用添加()
,插入()
,删除()
和明确()
的适配器。
For an ArrayAdapter
, notifyDataSetChanged
only works if you use the add()
, insert()
, remove()
, and clear()
on the Adapter.
当一个 ArrayAdapter
构造,它保存了列表
中传递的参考。如果你是通过在列表
这是一个活动中的一员,并更改活动成员后, ArrayAdapter
仍拿着参照原有列表
。适配器不知道你在活动改变了列表
。
When an ArrayAdapter
is constructed, it holds the reference for the List
that was passed in. If you were to pass in a List
that was a member of an Activity, and change that Activity member later, the ArrayAdapter
is still holding a reference to the original List
. The Adapter does not know you changed the List
in the Activity.
您的选择是:
- 使用的
ArrayAdapter
修改底层列表(添加()
,插入()
,删除()
,明确()
,等等。) - 重新创建
ArrayAdapter
新列表
数据。 (使用了大量的资源和垃圾收集。) - 创建从
BaseAdapter
派生自己的类和ListAdapter
,允许改变的基本列表
数据结构。 - 使用
notifyDataSetChanged()
每次更新列表的时间。要在UI线程调用它,使用runOnUiThread()
活动的
。 然后,notifyDataSetChanged()
将工作。
- Use the functions of the
ArrayAdapter
to modify the underlying List (add()
,insert()
,remove()
,clear()
, etc.) - Re-create the
ArrayAdapter
with the newList
data. (Uses a lot of resources and garbage collection.) - Create your own class derived from
BaseAdapter
andListAdapter
that allows changing of the underlyingList
data structure. - Use the
notifyDataSetChanged()
every time the list is updated. To call it on the UI-Thread, use therunOnUiThread()
ofActivity
. Then,notifyDataSetChanged()
will work.
这篇关于notifyDataSetChanged例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!