How to remove listview all items

后端 未结 11 2045
野的像风
野的像风 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<String> list = new ArrayList<String>();
    ArrayAdapter adapter;
    
    
    adapter = new ArrayAdapter<String>(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();
    
    0 讨论(0)
  • 2020-12-15 16:37

    You can only use

     lv.setAdapter(null);
    
    0 讨论(0)
  • 2020-12-15 16:37

    I used this statement and it worked for me:

       setListAdapter(null)
    

    This one calls a default constructor that does nothing in a class extends BaseAdapter.

    0 讨论(0)
  • 2020-12-15 16:38

    use any one of the bellow options which suites your requirement

    listview.removeViews(1,listview.getChildCount());
    

    or

    listview.removeViewInLayout(your view);
    
    0 讨论(0)
  • 2020-12-15 16:38

    An other approach after trying the solutions below. When you need it clear, just initialise your list to new clear new list.

    List<ModelData> dataLists = new ArrayList<>();
                    RaporAdapter adapter = new RaporAdapter(AyrintiliRapor.this, dataLists);
                    listview.setAdapter(adapter);
    

    Or set visibility to Gone / Invisible up to need

    img_pdf.setVisibility(View.INVISIBLE);
    
    0 讨论(0)
提交回复
热议问题