[android] Remove all items from RecyclerView

I am trying to remove all the elements from my RecyclerView in my onRestart method so the items don't get loaded twice:

@Override
protected void onRestart() {
    super.onRestart();

    // first clear the recycler view so items are not populated twice
    for (int i = 0; i < recyclerAdapter.getSize(); i++) {
        recyclerAdapter.delete(i);
    }

    // then reload the data
    PostCall doPostCall = new PostCall(); // my AsyncTask... 
    doPostCall.execute();
}

But for some reason the delete method I created in the adapter is not functioning properly:

in RecyclerAdapter.java:

public void delete(int position){
    myList.remove(position);
    notifyItemRemoved(position);
}

public int getSize(){
    return myList.size();
}

I think every other item in my list gets deleted instead of the entire list.

With a listview it was so easy and I simply called adapter.clear().

Can someone please help me fix up the code?

I think I should be using notifyItemRangeRemoved(...,...); but I am not sure how. TIA

This question is related to android adapter android-recyclerview

The answer is


Avoid deleting your items in a for loop and calling notifyDataSetChanged in every iteration. Instead just call the clear method in your list myList.clear(); and then notify your adapter

public void clearData() {
    myList.clear(); // clear list
    mAdapter.notifyDataSetChanged(); // let your adapter know about the changes and reload view.
}

private void clearRecyclerView() {
    CustomListViewValuesArr.clear();
    customRecyclerViewAdapter.notifyDataSetChanged();
}

use this func


recyclerView.removeAllViewsInLayout();

The above line would help you remove all views from the layout.

For you:

@Override
protected void onRestart() {
    super.onRestart();

    recyclerView.removeAllViewsInLayout(); //removes all the views

    //then reload the data
    PostCall doPostCall = new PostCall(); //my AsyncTask... 
    doPostCall.execute();
}

Help yourself:

public void clearAdapter() {
    arrayNull.clear();
    notifyDataSetChanged();
}

setAdapter(null);

Useful if RecycleView have different views type


You have better clear the array-list that you used for recyleview adapter.

arraylist.clear();

Another way to clear the recycleview items is to instanciate a new empty adapter.

mRecyclerView.setAdapter(new MyAdapter(this, new ArrayList<MyDataSet>()));

It's probably not the most optimized solution but it's working like a charm.


This is how I cleared my recyclerview and added new items to it with animation:

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

mSwipeRefreshLayout.setRefreshing(false);

//reset adapter with empty array list (it did the trick animation)
mAdapter = new MyAdapter(context, mList);
recyclerView.setAdapter(mAdapter);

mList.addAll(newList);
mAdapter.notifyDataSetChanged();

On Xamarin.Android, It works for me and need change layout

 var layout = recyclerView.GetLayoutManager() as GridLayoutManager;
 layout.SpanCount = GetItemPerRow(Context);
 recyclerView.SetAdapter(null);
 recyclerView.SetAdapter(adapter); //reset

ListView uses clear().

But, if you're just doing it for RecyclerView. First you have to clear your RecyclerView.Adapter with notifyItemRangeRemoved(0,size)

Then, only you recyclerView.removeAllViewsInLayout().


For my case adding an empty list did the job.

List<Object> data = new ArrayList<>();
adapter.setData(data);
adapter.notifyDataSetChanged();

public void clearData() {
    mylist.removeAll(mylist);
    mAdapter.notifyDataSetChanged();
    recyclerView.setAdapter(mAdapter);
}

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 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"

Examples related to android-recyclerview

Failed to resolve: com.android.support:cardview-v7:26.0.0 android CardView background color always white Changing background color of selected item in recyclerview Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView) Simple Android RecyclerView example Android Horizontal RecyclerView scroll Direction Margin between items in recycler view Android How to add a recyclerView inside another recyclerView RecyclerView - Get view at particular position Recyclerview inside ScrollView not scrolling smoothly