[android] 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;
    }

}

This question is related to android listview adapter

The answer is


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


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


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

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

 yourCollection.clear();
 setListAdapter(null);

Call clear() method from your custom adapter .


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

    **
 }

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

Below is link click


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


Simple its works me:)

YourArrayList_Object.clear();

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to listview

How to add a ListView to a Column in Flutter? Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView` Best way to update data with a RecyclerView adapter Android Recyclerview vs ListView with Viewholder Get clicked item and its position in RecyclerView How to show an empty view with a RecyclerView? NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference RecyclerView vs. ListView How to implement endless list with RecyclerView? Recyclerview and handling different type of row inflation

Examples related to adapter

Bridged networking not working in Virtualbox under Windows 10 Remove all items from RecyclerView NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference OpenVPN failed connection / All TAP-Win32 adapters on this system are currently in use Call Activity method from adapter Setting network adapter metric priority in Windows 7 How to remove listview all items How to start Activity in adapter? Clear listview content? Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"