Remove all items from RecyclerView
AndroidAdapterAndroid RecyclerviewAndroid Problem Overview
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
Android Solutions
Solution 1 - Android
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);
}
}
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();
}
Solution 2 - Android
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.
}
Solution 3 - Android
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();
}
Solution 4 - Android
setAdapter(null);
Useful if RecycleView have different views type
Solution 5 - Android
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();
Solution 6 - Android
Help yourself:
public void clearAdapter() {
arrayNull.clear();
notifyDataSetChanged();
}
Solution 7 - Android
I use this. This actually clears the recylerview completely. I had tried adapter.notifyDataSetChanged();
but it was just not updating any view in my case. Of course U had cleared the list first. But below code works fine and clears view of recyclerview completely.
listName.clear(); // clear list
adapter = new ModelAdaptor(Activity.this, listName);
recyclerView.setAdapter(adapter);
Solution 8 - Android
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.
Solution 9 - Android
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()
.
Solution 10 - Android
private void clearRecyclerView() {
CustomListViewValuesArr.clear();
customRecyclerViewAdapter.notifyDataSetChanged();
}
use this func
Solution 11 - Android
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
Solution 12 - Android
For my case adding an empty list did the job.
List<Object> data = new ArrayList<>();
adapter.setData(data);
adapter.notifyDataSetChanged();
Solution 13 - Android
You have better clear the array-list that you used for recyleview adapter.
arraylist.clear();
Solution 14 - Android
public void clearData() {
mylist.removeAll(mylist);
mAdapter.notifyDataSetChanged();
recyclerView.setAdapter(mAdapter);
}