How to clear an Android ListView and populate it with new data?

99封情书 提交于 2021-01-21 07:37:35

问题


I have a spinner and a listview in the same Activity. The listview is loaded with a list of items. i want when user clicks on the item from spinner the listview will be clear and new listview will load according to item chooses in spinner

    setListAdapter(new ArrayAdapter<String>(this, R.layout.listviewxml, video));

       ListView lv=getListView();

       lv.setTextFilterEnabled(true);

enter image description here

Please tell how to do this?

i am not able to clear the listview


回答1:


If you have passed a List or an Array to the Adapter, then

// Clear collection..
collection.clear();
// Add data to collection..
collection.add();
// Refresh your listview..
listview.getAdapter().notifyDataSetChanged();

(These codes are like pseudo code, your syntax may vary)




回答2:


When a new item is chosen in your Spinner use on your ListView adapter :

    //Clear existing items
    listAdapter.clear();
    //Add new items
    listAdapter.addAll(collectionOfItems);
    //Notify that the data has changed
    listAdapter.notifyDataSetChanged();



回答3:


You could just try setting a null adapter to the listview. So you could just do :

    lv.setadapter(null);



回答4:


You should store a reference to the Adapter and perform clear on that. Then you can add more items again using the Adapter.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listviewxml, video);
setListAdapter(adapter);

then later use

adapter.clear();



回答5:


Try this. its works for me:)

 YourArrayList_Object.clear(); // clear the listview 

or

ArrayList<DetailsForCallhistoryListActivity> callhistory_details_list; 

then

callhistory_details_list.clear();


来源:https://stackoverflow.com/questions/8310851/how-to-clear-an-android-listview-and-populate-it-with-new-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!