Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 707
Sunny Avatar asked Nov 29 '11 12:11

Sunny


3 Answers

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)

like image 134
user370305 Avatar answered Oct 14 '22 20:10

user370305


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();
like image 28
Ovidiu Latcu Avatar answered Oct 14 '22 20:10

Ovidiu Latcu


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

    lv.setadapter(null);
like image 30
Aditya Avatar answered Oct 14 '22 18:10

Aditya