Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 684
user2456977 Avatar asked Apr 30 '15 22:04

user2456977


People also ask

How do I empty the RecyclerView cache?

Only way to do is by using notifyDataSetChanged for all items. Recyclerview will remove views from cache and add those to recyler views pool. So now, when LM asks for view, either recycled view(according to itemType) or newly created view will be returned, and adapter will bind data to these views.

How to add delete button to recycler view item layout?

Let’s add one delete button to the recycler view item layout list_item.xml. It looks as like below : The code for this xml file is as below: Inside the adapter class, we will get the reference of the delete button and on click of this button, we will call one function that deletes the data:

How to clear recyclerview items?

Another way to clear the recycleview items is to instanciate a new empty adapter. It's probably not the most optimized solution but it's working like a charm. ListView uses clear (). But, if you're just doing it for RecyclerView. First you have to clear your RecyclerView.Adapter with notifyItemRangeRemoved (0,size)

How to delete an item from list data using adapter class?

Inside the adapter class, we will get the reference of the delete button and on click of this button, we will call one function that deletes the data: Inside the bind method, we are adding one click listener to the delete button. It calls deleteItem with the index. In deleteItem, we are removing the item from listData at position index.

How to remove an item from list data at position index?

Inside the bind method, we are adding one click listener to the delete button. It calls deleteItem with the index. In deleteItem, we are removing the item from listData at position index.


1 Answers

This works great for me:

public void clear() {     int size = data.size();     if (size > 0) {         for (int i = 0; i < size; i++) {             data.remove(0);         }          notifyItemRangeRemoved(0, size);     } } 

Source: https://github.com/mikepenz/LollipopShowcase/blob/master/app/src/main/java/com/mikepenz/lollipopshowcase/adapter/ApplicationAdapter.java

or:

public void clear() {     int size = data.size();     data.clear();     notifyItemRangeRemoved(0, size); } 

For you:

@Override protected void onRestart() {     super.onRestart();      // first clear the recycler view so items are not populated twice     recyclerAdapter.clear();      // then reload the data     PostCall doPostCall = new PostCall(); // my AsyncTask...      doPostCall.execute(); } 
like image 196
Jared Burrows Avatar answered Sep 22 '22 23:09

Jared Burrows