78

I have a little problem with ListView. How do I clear a ListView content, knowing that it has a custom adapter?

edit - the custom adapter class extends BaseAdapter, it looks like this:

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater = null;

    public MyAdapter(Activity a, String[] str) {
        activity = a;
        data = str;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder {
        public TextView text;
    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        View v = view;
        ViewHolder holder;
        if (v == null) {
            v = inflater.inflate(R.layout.rowa, null);
            holder = new ViewHolder();
            holder.text= v.findViewById(R.id.dexter);
            v.setTag(holder);
        } else {
            holder = v.getTag();
        }

        holder.text.setText(data[position]);

        return v;
    }

}
zeroDivider
  • 1,050
  • 13
  • 29
Mohamed Gallah
  • 1,158
  • 1
  • 10
  • 16

10 Answers10

203

Simply write

listView.setAdapter(null);
mikepenz
  • 12,708
  • 14
  • 77
  • 117
56

I guess you passed a List or an Array to the Adapter. If you keep the instance of this added collection, you can do a

collection.clear();
listview.getAdapter().notifyDataSetChanged();

this'll work only if you instantiated the adapter with collection and it's the same instance.

Also, depending on the Adapter you extended, you may not be able to do this. SimpleAdapter is used for static data, thus it can't be updated after creation.

PS. not all Adapters have a clear() method. ArrayAdapter does, but ListAdapter or SimpleAdapter don't

Maragues
  • 37,861
  • 14
  • 95
  • 96
  • i'm extending BaseAdapter and i think it hasn't clear() method. – Mohamed Gallah Sep 28 '10 at 09:25
  • Do you need it to be a BaseAdapter? If you could switch to extending an ArrayAdapter, you would get the clear() method. Looks like in BaseAdapter you need to use this registerDataSetObserver(DataSetObserver observer), I guess it'll update automatically if the DataSet changed. But, ArrayAdapter will probable make things easier. Good luck! – Maragues Sep 28 '10 at 11:29
  • thanks for help but can you give me a sample code on how to extend ArrayAdapter. thanks. – Mohamed Gallah Sep 28 '10 at 16:17
  • check http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass It's really simillar to what you already have. – Maragues Sep 30 '10 at 07:56
  • thanks for sharing but i seems that my problem isn't related to the adapter, because even when i use the ArrayAdapter's clear() and i reload the activity, the previous content remains there in addition to the new content. i'm in real trouble. (Sorry for bad english, i hope you get the point). – Mohamed Gallah Sep 30 '10 at 11:25
  • Do you override the adapter data set or the adapter after creating it? That is, do you re assign the instances you used to add the adapter to the list? If these instances change, the connection between the adapter, the listview and the screen may fail. – Maragues Sep 30 '10 at 14:06
  • Sorry but i don't get it, can you please explain more! – Mohamed Gallah Sep 30 '10 at 16:28
  • it wasn't a n adapter problem, it's XML parsing problem. thaks a lot guys for help. – Mohamed Gallah Oct 04 '10 at 15:16
8

It's simple .First you should clear your collection and after clear list like this code :

 yourCollection.clear();
 setListAdapter(null);
zheek
  • 742
  • 1
  • 11
  • 22
3

As of Android versions M and N, following works for me and would be the correct approach. Emptying the ListView or setting the Adapter to null is not the right approach and would lead to null pointer issue, invalid ListView and/or crash of the app.

Simply do:

    mList.clear();
    mAdapter.notifyDataSetChanged();

i.e. first you clear the list altogether, and then let the adapter know about this change. Android will take care of correctly updating the UI with an empty list. In my case, my list is an ArrayList.

In case you are doing this operation from a different thread, run this code on the UI thread:

    runOnUiThread(mRunnable);

Where mRunnable would be:

    Runnable mRunnable = new Runnable() {
        public void run() {
            mList.clear();
            mAdapter.notifyDataSetChanged();
        }
    };;
zeeshan
  • 4,913
  • 1
  • 49
  • 58
1

Simple its works me:)

YourArrayList_Object.clear();
Agilanbu
  • 2,747
  • 2
  • 28
  • 33
  • Could you elaborate little bit. – Panther Dec 15 '16 at 07:58
  • ArrayList callhistory_details_list; using this arraylist object (i.e callhistory_details_list) . callhistory_details_list.clear(); hope it will help u. let me know any queries – Agilanbu Dec 16 '16 at 09:37
0

There is a solution for the duplicate entry in listview. You have to declare the onBackPress()-method on your activity and write down the highlight code given below:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    **

    attendence_webdata.clear(); list.setAdapter(null);
    --------------------------------------------------

    **
 }
Ionic
  • 3,884
  • 1
  • 12
  • 33
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
  • LOL, of course not, what duplicate entry are you talking about ? What does onBackPressed has to do with clearing adapters ? Do you even know what you are talking about ? – 2Dee Jul 09 '15 at 08:21
  • Sry for my english but when i click button and open screen for list view every time it come with same entry so use clear method in back press – Arpit Patel Jul 10 '15 at 07:29
0

Remove your items from your custom adapter and call notifyDataSetChanged().

mreichelt
  • 12,359
  • 6
  • 56
  • 70
-1

Just put the code ListView.Items.Clear(); on your method

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
-3

Call clear() method from your custom adapter .

zheek
  • 742
  • 1
  • 11
  • 22
carlovv
  • 222
  • 2
  • 10
-4

You need to call both clear() from ArrayAdapter and notifyDataSetChanged() both.

Below is link click

zheek
  • 742
  • 1
  • 11
  • 22
Hare-Krishna
  • 1,425
  • 3
  • 16
  • 26