【问题标题】:How to clear ListView, it just adds new content after old content in Android App?如何清除ListView,它只是在Android App中的旧内容之后添加新内容?
【发布时间】:2015-11-25 22:56:52
【问题描述】:

请给我一些建议。

我正在尝试将内容放入 Android 中的 ListView。

我传入一个 ArrayList 类型的 String,设置一个适配器来传入该内容,然后将适配器传递给 listView。

问题出现在内容确实显示的事实中,但是当我每次运行活动时,新内容只是列在旧内容之上。例如

活动名称

item 1
item 2
---------
item 1 //repeats again but with new content
item 2
item 3

我试过了:

list.setAdapter(null);
arrayAdapter.clear();
arrayAdapter.notifyDataSetChanged();;

这些似乎都不起作用。强制关闭应用程序确实绘制了正确的内容。

有什么问题欢迎提问。

public void fillListView(){


        arrayAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                StartSearch.myList22 );              //fill adapter with content


        lv = (ListView)findViewById(R.id.listView2);

        lv.setAdapter(arrayAdapter);    //feed data to listview
    }

这个方法在每次打开activity时都会顺便运行一次,以刷新内容。

【问题讨论】:

  • 你需要清除StartSearch.myList22数组

标签: java android listview android-listview android-arrayadapter


【解决方案1】:

做你的

 StartSearch.myList22 as a Global variable and 

--> 当您获得新数据时,请使用您的列表尝试上述答案,如下代码:-

 StartSearch.myList22.clear();
 StartSearch.myList22.addall(NewList);

--> 如果还是不行,试试下面的代码:-

 StartSearch.myList22.clear();
 StartSearch.myList22.addall(NewList);
 arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            StartSearch.myList22 ); 
lv.setAdapter(arrayAdapter);

-谢谢!~!

【讨论】:

    【解决方案2】:

    对不起,让我说得更具体一些。 我认为您不必将适配器设置为 null 并清除它

    您可以更改设置为适配器的数组列表,如

    yourList.clear();
    yourList.add(data);
    

    一旦您的数据集发生变化,然后调用:

     yourAdapter.notifyDataSetChanged(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 2019-07-21
      • 1970-01-01
      相关资源
      最近更新 更多