Android notifyDataSetChanged 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3669325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:17:02  来源:igfitidea点击:

notifyDataSetChanged example

androidandroid-arrayadapternotifydatasetchanged

提问by Tima

I'm trying to use in my Android Applicationthe notifyDataSetChanged()method for an ArrayAdapterbut it doesn't work for me.

我正在尝试在我Android ApplicationnotifyDataSetChanged()方法中使用 an,ArrayAdapter但它对我不起作用。

I found as answer here, that notifyDataSetChanged()should run in the main thread, but there was no example for that.

我在这里找到答案,它notifyDataSetChanged()应该在主线程中运行,但没有例子。

Could anybody send an example or at least a link?!

任何人都可以发送一个示例或至少一个链接?!

回答by jnosek

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

对于ArrayAdapternotifyDataSetChanged只有当你使用的作品add()insert()remove(),和clear()在适配器。

When an ArrayAdapteris constructed, it holds the reference for the Listthat was passed in. If you were to pass in a Listthat was a member of an Activity, and change that Activity member later, the ArrayAdapteris still holding a reference to the original List. The Adapter does not know you changed the Listin the Activity.

ArrayAdapter构造an 时,它保存List传入的 的引用。如果您要传入ListActivity 成员的 ,并且稍后更改该 Activity 成员,ArrayAdapter仍然持有对原始 的引用List。适配器不知道您List在 Activity 中更改了。

Your choices are:

您的选择是:

  1. Use the functions of the ArrayAdapterto modify the underlying List (add(), insert(), remove(), clear(), etc.)
  2. Re-create the ArrayAdapterwith the new Listdata. (Uses a lot of resources and garbage collection.)
  3. Create your own class derived from BaseAdapterand ListAdapterthat allows changing of the underlying Listdata 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.
  1. 使用的功能ArrayAdapter修改底层列表(add()insert()remove()clear(),等等)
  2. ArrayAdapter使用新List数据重新创建。(使用大量资源和垃圾收集。)
  3. 创建自己的类衍生BaseAdapterListAdapter允许潜在的改变List数据结构。
  4. notifyDataSetChanged()每次更新列表时使用。要在 UI 线程上调用它,请使用runOnUiThread()of Activity。然后,notifyDataSetChanged()将工作。

回答by Brian

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.

您可以使用以下runOnUiThread()方法。如果您不使用ListActivity,只需修改代码以获取对您的ArrayAdapter.

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

回答by Arif Nadeem

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.

我最近写了关于这个主题的文章,虽然这篇文章很旧,但我认为这对想知道如何以BaseAdapter.notifyDataSetChanged()正确的方式逐步实施的人会有所帮助。

Please follow How to correctly implement BaseAdapter.notifyDataSetChanged() in Androidor the newer blog BaseAdapter.notifyDataSetChanged().

请遵循如何在 Android 中正确实现 BaseAdapter.notifyDataSetChanged()更新的博客 BaseAdapter.notifyDataSetChanged()

回答by Kevin Parker

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.

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

Add this where you would normally (try to) call notify

将此添加到您通常(尝试)调用通知的位置

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

AdapterHelper class

AdapterHelper 类

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

回答by mutp

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.

我知道这是一个迟到的回应,但我遇到了类似的问题,我设法通过notifyDataSetChanged()在正确的地方使用来解决它。

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.

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

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 FragmentTab2and it would directly update my listview in FragmentTab2.

此活动将返回一些数据,FragmentTab2并将直接更新我在FragmentTab2.

Hope someone finds this useful!

希望有人觉得这很有用!