通知数据集更改示例

发布于 2024-09-18 10:21:01 字数 268 浏览 6 评论 0原文

我尝试在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

标点 2024-09-25 10:21:01

对于 ArrayAdapternotifyDataSetChanged 仅在使用 add()insert()时才有效适配器上的remove()clear()

当构造 ArrayAdapter 时,它会保存传入的 List 的引用。如果您要传入一个 List,它是Activity 的成员,并且稍后更改该 Activity 成员,ArrayAdapter 仍保留对原始 List 的引用。适配器不知道您更改了活动中的List

您的选择是:

  1. 使用 ArrayAdapter 的函数来修改底层 List(add()insert()remove( )clear() 等)
  2. 使用新的 List 数据重新创建 ArrayAdapter。 (使用大量资源和垃圾收集。)
  3. 创建您自己的从 BaseAdapterListAdapter 派生的类,允许更改底层 List 数据结构。
  4. 每次更新列表时,请使用 notifyDataSetChanged()。要在 UI 线程上调用它,请使用 ActivityrunOnUiThread()
    然后,notifyDataSetChanged() 将起作用。

For an ArrayAdapter, notifyDataSetChanged only works if you use the add(), insert(), remove(), and clear() on the Adapter.

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.

Your choices are:

  1. Use the functions of the ArrayAdapter to modify the underlying List (add(), insert(), remove(), clear(), etc.)
  2. Re-create the ArrayAdapter with the new List data. (Uses a lot of resources and garbage collection.)
  3. Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure.
  4. Use the notifyDataSetChanged() every time the list is updated. To call it on the UI-Thread, use the runOnUiThread() of Activity.
    Then, notifyDataSetChanged() will work.
窗影残 2024-09-25 10:21:01

您可以使用 runOnUiThread()方法如下。如果您不使用 ListActivity,只需调整代码即可获取对 ArrayAdapter 的引用。

final ArrayAdapter adapter = ((ArrayAdapter)getListAdapter());
runOnUiThread(new Runnable() {
    public void run() {
        adapter.notifyDataSetChanged();
    }
});

You can use the runOnUiThread() method as follows. If you're not using a ListActivity, just adapt the code to get a reference to your ArrayAdapter.

final ArrayAdapter adapter = ((ArrayAdapter)getListAdapter());
runOnUiThread(new Runnable() {
    public void run() {
        adapter.notifyDataSetChanged();
    }
});
唐婉 2024-09-25 10:21:01

我最近写了关于这个主题的文章,虽然这篇文章很旧,但我认为这对于那些想知道如何一步步以正确的方式实现 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().

情仇皆在手 2024-09-25 10:21:01

我遇到了同样的问题,我不想连续用新实例替换整个 ArrayAdapter。因此,我让 AdapterHelper 在其他地方完成繁重的工作。

将其添加到您通常(尝试)调用通知

new AdapterHelper().update((ArrayAdapter)adapter, new ArrayList<Object>(yourArrayList));
adapter.notifyDataSetChanged();

AdapterHelper 类的位置

public class AdapterHelper {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void update(ArrayAdapter arrayAdapter, ArrayList<Object> listOfObject){
        arrayAdapter.clear();
        for (Object object : listOfObject){
            arrayAdapter.add(object);
        }
    }
}

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

new AdapterHelper().update((ArrayAdapter)adapter, new ArrayList<Object>(yourArrayList));
adapter.notifyDataSetChanged();

AdapterHelper class

public class AdapterHelper {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void update(ArrayAdapter arrayAdapter, ArrayList<Object> listOfObject){
        arrayAdapter.clear();
        for (Object object : listOfObject){
            arrayAdapter.add(object);
        }
    }
}
献世佛 2024-09-25 10:21:01

我知道这是一个迟到的回复,但我面临着类似的问题,我设法通过在正确的位置使用 notifyDataSetChanged() 来解决它。

所以我的情况如下。

我必须使用从完全不同的活动返回的内容来更新操作栏选项卡(片段)中的列表视图。但最初,列表视图不会反映任何更改。但是,当我单击另一个选项卡然后返回所需的选项卡时,列表视图将使用其他活动的正确内容进行更新。因此,为了解决这个问题,我在必须返回数据的活动代码中使用了操作栏适配器的 notifyDataSetChanged()

这是我在活动中使用的代码片段。

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) 
    {
        case R.id.action_new_forward:

            FragmentTab2.mListAdapter.notifyDataSetChanged();//this updates the adapter in my action bar tab
            Intent ina = new Intent(getApplicationContext(), MainActivity.class);
            ina.putExtra("stra", values1);
            startActivity(ina);// This is the code to start the parent activity of my action bar tab(fragment).
    }
}

此活动将向 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.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) 
    {
        case R.id.action_new_forward:

            FragmentTab2.mListAdapter.notifyDataSetChanged();//this updates the adapter in my action bar tab
            Intent ina = new Intent(getApplicationContext(), MainActivity.class);
            ina.putExtra("stra", values1);
            startActivity(ina);// This is the code to start the parent activity of my action bar tab(fragment).
    }
}

This activity would return some data to FragmentTab2 and it would directly update my listview in FragmentTab2.

Hope someone finds this useful!

等风也等你 2024-09-25 10:21:01

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()

  • If you change multiple data at a time then you need to apply notifyDataSetChanged().

2. notifyItemInserted ( position : Int )

  • If you Insert data oneByOne in your arrayList then you don't need to do notifyDataSetChanged() because whenever you called notifyDataSetChanged() it will call onBindViewHolder() as many times as your arrayList size. And that's not 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 use yourAdapter.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 )

  • when you remove item from a particular position in a list then you need to use notifyItemRemoved ( position : Int ) method to notify the adapter that update your list and removed this item at this position.

4. notifyItemChanged ( position : Int )

  • when you update your list at a particular position then you need to notify the adapter with notifyItemChanged() with a parameter of a position that you were updated.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文