Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear listview content?

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;
    }

}
like image 323
Mohamed Gallah Avatar asked Sep 27 '10 08:09

Mohamed Gallah


People also ask

How to Clear ListView?

Call clear() method from your custom adapter . clean() is not available with BaseAdapter. you can also set the listview adapter to null.


5 Answers

Simply write

listView.setAdapter(null);
like image 55
mikepenz Avatar answered Oct 07 '22 16:10

mikepenz


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

like image 36
Maragues Avatar answered Oct 07 '22 15:10

Maragues


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

 yourCollection.clear();
 setListAdapter(null);
like image 33
zheek Avatar answered Oct 07 '22 14:10

zheek


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();
        }
    };;
like image 38
zeeshan Avatar answered Oct 07 '22 15:10

zeeshan


Simple its works me:)

YourArrayList_Object.clear();
like image 45
Agilanbu Avatar answered Oct 07 '22 15:10

Agilanbu