How to remove listview all items
Asked Answered
P

12

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 remove the listview items dynamically.

Pepsinogen answered 13/9, 2011 at 10:37 Comment(3)
dynamically pass getcount, to clear pass zero as getcountEgg
listview.setAdapter(null);Vendue
Thank you Abhi, that really helped. Overriding the getCount method ....Bickerstaff
B
39

Call setListAdapter() again. This time with an empty ArrayList.

Benthamism answered 13/9, 2011 at 10:41 Comment(3)
Here if you click on the button old data listview item will be removed after that new items will be added to the list view. is it possible with this?Pepsinogen
Yes, you can call setListAdaptor() with new data as needed.Benthamism
Calling setListAdapter() with a new Adapter object would be expensive. Just remove all the items in the adapter using clear() and add the new data to existing Adapter object using add()/addAll() and call notifyDatasetChanged().Truant
D
26

ListView operates based on the underlying data in the Adapter. In order to clear the ListView you need to do two things:

  1. Clear the data that you set from adapter.
  2. Refresh the view by calling notifyDataSetChanged

For example, see the skeleton of SampleAdapter below that extends the BaseAdapter


public class SampleAdapter extends BaseAdapter {

    ArrayList<String> data;

    public SampleAdapter() {
        this.data = new ArrayList<String>();
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // your View
        return null;
    }
}

Here you have the ArrayList<String> data as the data for your Adapter. While you might not necessary use ArrayList, you will have something similar in your code to represent the data in your ListView

Next you provide a method to clear this data, the implementation of this method is to clear the underlying data structure


public void clearData() {
    // clear the data
    data.clear();
}

If you are using any subclass of Collection, they will have clear() method that you could use as above.

Once you have this method, you want to call clearData and notifyDataSetChanged on your onClick thus the code for onClick will look something like:


// listView is your instance of your ListView
SampleAdapter sampleAdapter = (SampleAdapter)listView.getAdapter();
sampleAdapter.clearData();

// refresh the View
sampleAdapter.notifyDataSetChanged();
Dogmatics answered 13/9, 2011 at 11:22 Comment(0)
E
10

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();
Eolith answered 13/9, 2011 at 10:43 Comment(1)
i am using the base adapter. so i don't have clear() metthod. how to remove it?Pepsinogen
U
8

You can only use

 lv.setAdapter(null);
Undesigning answered 18/3, 2015 at 22:39 Comment(0)
C
5

You can do this:

listView.setAdapter(null);
Concrescence answered 20/6, 2018 at 17:7 Comment(0)
S
4

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.

Septimal answered 24/5, 2012 at 12:31 Comment(1)
this will break android 2.1Alaska
S
3

I just clean the arraylist , try values.clear();

values = new ArrayList<String>();
values.clear();

ArrayAdapter <String> adapter;
adapter = new ArrayAdapter<String>(this, R.layout.list,android.R.id.text1, values); 
lista.setAdapter(adapter);
Slinkman answered 22/9, 2014 at 20:6 Comment(0)
G
0

Remove the data from the adapter and call adapter.notifyDataSetChanged();

Gibbeon answered 13/9, 2011 at 10:40 Comment(3)
how to remove the data from the adpater?Pepsinogen
while declaring adapter you would have passed values in some form...remove the value from there....Gibbeon
That is the question. How to remove the items from the adapter.Truant
J
0

use any one of the bellow options which suites your requirement

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

or

listview.removeViewInLayout(your view);
Jopa answered 13/9, 2011 at 11:10 Comment(0)
H
0

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);
Hebron answered 21/3, 2018 at 21:21 Comment(0)
P
0

For me worked this way:

private ListView yourListViewName;
private List<YourClassName> yourListName;

  ...

yourListName = new ArrayList<>();
yourAdapterName = new yourAdapterName(this, R.layout.your_layout_name, yourListName);

  ...

if (yourAdapterName.getCount() > 0) {
   yourAdapterName.clear();
   yourAdapterName.notifyDataSetChanged();
}

yourAdapterName.add(new YourClassName(yourParameter1, yourParameter2, ...));
yourListViewName.setAdapter(yourAdapterName);
Prismatoid answered 23/3, 2018 at 18:27 Comment(0)
B
0

//very simple

adapter.clear();

Bully answered 14/9, 2021 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.