How to remove listview all items

后端 未结 11 2135
野的像风
野的像风 2020-12-15 15:36

In my app, if you click on a button then i want to remove all the listview items. Here, i am using the base adapter for adding the items to the list view.

How can i

11条回答
  •  一生所求
    2020-12-15 16:30

    if you used List object and passed to the adapter you can remove the value from the List object and than call the notifyDataSetChanged() using adapter object.

    for e.g.

    List list = new ArrayList();
    ArrayAdapter adapter;
    
    
    adapter = new ArrayAdapter(DeleteManyTask.this, 
                android.R.layout.simple_list_item_1,
                (String[])list.toArray(new String[0]));
    
    listview = (ListView) findViewById(R.id.list);
    listview.setAdapter(adapter);
    
    listview.setAdapter(listAdapter);
    

    for remove do this way

    list.remove(index); //or
    list.clear();
    adpater.notifyDataSetChanged();
    

    or without list object remove item from list.

    adapter.clear();
    adpater.notifyDataSetChanged();
    

提交回复
热议问题