【问题标题】:How to clear an Android ListView and populate it with new data?如何清除 Android ListView 并用新数据填充它?
【发布时间】:2012-01-08 19:09:08
【问题描述】:
我在同一个 Activity 中有一个微调器和一个列表视图。列表视图加载了项目列表。我希望当用户从微调器中单击项目时,列表视图将被清除,并且新的列表视图将根据微调器中的项目选择加载
setListAdapter(new ArrayAdapter<String>(this, R.layout.listviewxml, video));
ListView lv=getListView();
lv.setTextFilterEnabled(true);
请告诉我该怎么做?
我无法清除列表视图
【问题讨论】:
标签:
android
listview
sqlite
【解决方案1】:
如果你已经向适配器传递了一个列表或数组,那么
// Clear collection..
collection.clear();
// Add data to collection..
collection.add();
// Refresh your listview..
listview.getAdapter().notifyDataSetChanged();
(这些代码类似于伪代码,您的语法可能会有所不同)
【解决方案2】:
当您在 Spinner 中选择新项目时,请在您的 ListView 适配器上使用:
//Clear existing items
listAdapter.clear();
//Add new items
listAdapter.addAll(collectionOfItems);
//Notify that the data has changed
listAdapter.notifyDataSetChanged();
【解决方案3】:
您可以尝试为列表视图设置一个空适配器。所以你可以这样做:
lv.setadapter(null);
【解决方案4】:
您应该存储对适配器的引用并对其执行 clear 操作。然后您可以使用适配器再次添加更多项目。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listviewxml, video);
setListAdapter(adapter);
以后再用
adapter.clear();
【解决方案5】:
试试这个。它对我有用:)
YourArrayList_Object.clear(); // clear the listview
或
ArrayList<DetailsForCallhistoryListActivity> callhistory_details_list;
那么
callhistory_details_list.clear();