通知数据集更改示例
我尝试在我的 Android 应用程序中使用 ArrayAdapter 的 notifyDataSetChanged() 方法,但它对我不起作用。
我发现这里的答案,notifyDataSetChanged()
应该在主线程中运行,但没有这方面的例子。
有人可以发送一个示例或至少一个链接吗?
I'm trying to use in my Android Application
the notifyDataSetChanged()
method for an ArrayAdapter
but it doesn't work for me.
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?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于
ArrayAdapter
,notifyDataSetChanged
仅在使用add()
、insert()
、时才有效适配器上的remove()
和clear()
。当构造
ArrayAdapter
时,它会保存传入的List
的引用。如果您要传入一个List
,它是Activity 的成员,并且稍后更改该 Activity 成员,ArrayAdapter
仍保留对原始List
的引用。适配器不知道您更改了活动中的List
。您的选择是:
ArrayAdapter
的函数来修改底层 List(add()
、insert()
、remove( )
、clear()
等)List
数据重新创建ArrayAdapter
。 (使用大量资源和垃圾收集。)BaseAdapter
和ListAdapter
派生的类,允许更改底层List
数据结构。notifyDataSetChanged()
。要在 UI 线程上调用它,请使用Activity
的runOnUiThread()
。然后,
notifyDataSetChanged()
将起作用。For an
ArrayAdapter
,notifyDataSetChanged
only works if you use theadd()
,insert()
,remove()
, andclear()
on the Adapter.When an
ArrayAdapter
is constructed, it holds the reference for theList
that was passed in. If you were to pass in aList
that was a member of an Activity, and change that Activity member later, theArrayAdapter
is still holding a reference to the originalList
. The Adapter does not know you changed theList
in the Activity.Your choices are:
ArrayAdapter
to modify the underlying List (add()
,insert()
,remove()
,clear()
, etc.)ArrayAdapter
with the newList
data. (Uses a lot of resources and garbage collection.)BaseAdapter
andListAdapter
that allows changing of the underlyingList
data structure.notifyDataSetChanged()
every time the list is updated. To call it on the UI-Thread, use therunOnUiThread()
ofActivity
.Then,
notifyDataSetChanged()
will work.您可以使用
runOnUiThread()
方法如下。如果您不使用ListActivity
,只需调整代码即可获取对ArrayAdapter
的引用。You can use the
runOnUiThread()
method as follows. If you're not using aListActivity
, just adapt the code to get a reference to yourArrayAdapter
.我最近写了关于这个主题的文章,虽然这篇文章很旧,但我认为这对于那些想知道如何一步步以正确的方式实现
BaseAdapter.notifyDataSetChanged()
的人来说会有帮助。请按照如何在Android中正确实现BaseAdapter.notifyDataSetChanged()或较新的博客 BaseAdapter.notifyDataSetChanged()。
I recently wrote on this topic, though this post it old, I thought it will be helpful to someone who wants to know how to implement
BaseAdapter.notifyDataSetChanged()
step by step and in a correct way.Please follow How to correctly implement BaseAdapter.notifyDataSetChanged() in Android or the newer blog BaseAdapter.notifyDataSetChanged().
我遇到了同样的问题,我不想连续用新实例替换整个 ArrayAdapter。因此,我让 AdapterHelper 在其他地方完成繁重的工作。
将其添加到您通常(尝试)调用通知
AdapterHelper 类的位置
I had the same problem and I prefer not to replace the entire ArrayAdapter with a new instance continuously. Thus I have the AdapterHelper do the heavy lifting somewhere else.
Add this where you would normally (try to) call notify
AdapterHelper class
我知道这是一个迟到的回复,但我面临着类似的问题,我设法通过在正确的位置使用
notifyDataSetChanged()
来解决它。所以我的情况如下。
我必须使用从完全不同的活动返回的内容来更新操作栏选项卡(片段)中的列表视图。但最初,列表视图不会反映任何更改。但是,当我单击另一个选项卡然后返回所需的选项卡时,列表视图将使用其他活动的正确内容进行更新。因此,为了解决这个问题,我在必须返回数据的活动代码中使用了操作栏适配器的
notifyDataSetChanged()
。这是我在活动中使用的代码片段。
此活动将向
FragmentTab2
返回一些数据,并直接更新FragmentTab2
中的列表视图。希望有人觉得这很有用!
I know this is a late response but I was facing a similar issue and I managed to solve it by using
notifyDataSetChanged()
in the right place.So my situation was as follows.
I had to update a listview in an action bar tab (fragment) with contents returned from a completely different activity. Initially however, the listview would not reflect any changes. However, when I clicked another tab and then returned to the desired tab,the listview would be updated with the correct content from the other activity. So to solve this I used
notifyDataSetChanged()
of the action bar adapter in the code of the activity which had to return the data.This is the code snippet which I used in the activity.
This activity would return some data to
FragmentTab2
and it would directly update my listview inFragmentTab2
.Hope someone finds this useful!
Recyclerview 中有多个通知程序,它们将在不同的事件中被调用。
1.notifyDataSetChanged()
notifyDataSetChanged()
。2.notifyItemInserted (position : Int )
如果您在 arrayList 中
插入数据 oneByOne
那么您不需要执行notifyDataSetChanged()
因为每当您调用notifyDataSetChanged()
它将调用onBindViewHolder()
次数,次数与arrayList 大小
相同。在我看来,这不是一个好的做法
。所以这就是为什么当您需要一项一项地插入项目时,总是需要使用yourAdapter.notifyItemInserted(position : Int)
并提供您插入数据的参数。大多数情况下,您需要在
notifyItemInserted(list.size)
中传递一个位置,因为您总是在 arrayList 的末尾插入数据。3.notifyItemRemoved (position: Int)
notifyItemRemoved (position: Int)
方法来通知适配器更新您的列表并删除该项目在这个位置。4.notifyItemChanged (position: Int)
notifyItemChanged()
通知适配器,并提供已更新位置的参数。Well There is multiple notifiers in Recyclerview that will be called at different events.
1. notifyDataSetChanged()
notifyDataSetChanged()
.2. notifyItemInserted ( position : Int )
If you
Insert data oneByOne
in your arrayList then you don't need to donotifyDataSetChanged()
because whenever you callednotifyDataSetChanged()
it will callonBindViewHolder()
as many times as yourarrayList size
. And that'snot good practice
in my point of view. so that's why when you need to insert item's one by one then always need to useyourAdapter.notifyItemInserted(position : Int)
and gives parameter that you inserted a data.Mostly you need to pass as a position in
notifyItemInserted(list.size)
because you always insert data at the end of the arrayList.3. notifyItemRemoved ( position : Int )
notifyItemRemoved ( position : Int )
method to notify the adapter that update your list and removed this item at this position.4. notifyItemChanged ( position : Int )
notifyItemChanged()
with a parameter of a position that you were updated.