Trying to figure out what is the issue with updating RecyclerView
\'s Adapter.
After I get a new List of products, I tried to:
Update t
If nothing mentioned in the above comments is working for you. It might mean the problem lies somewhere else.
One place I found the solution was in the way I was setting the list to the adapter. In my activity the list was a instance variable and I was changing it directly when any data changed. Due to it being a reference variable there was something weird going on. So I changed the reference variable to a local one and used another variable to update data and then pass to addAll()
function mentioned in above answers.
you have 2 options to do this: refresh UI from the adapter:
mAdapter.notifyDataSetChanged();
or refresh it from recyclerView itself:
recyclerView.invalidate();
I've solved the same problem in a different way. I don't have data I waiting for it from the background thread so start with an emty list.
mAdapter = new ModelAdapter(getContext(),new ArrayList<Model>());
// then when i get data
mAdapter.update(response.getModelList());
// and update is in my adapter
public void update(ArrayList<Model> modelList){
adapterModelList.clear();
for (Product model: modelList) {
adapterModelList.add(model);
}
mAdapter.notifyDataSetChanged();
}
That's it.
Update Data of listview, gridview and recyclerview
mAdapter.notifyDataSetChanged();
or
mAdapter.notifyItemRangeChanged(0, itemList.size());
I'm working with RecyclerView and both the remove and the update work well.
1) REMOVE: There are 4 steps to remove an item from a RecyclerView
list.remove(position);
recycler.removeViewAt(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, list.size());
These line of codes work for me.
2) UPDATE THE DATA: The only things I had to do is
mAdapter.notifyDataSetChanged();
You had to do all of this in the Actvity/Fragment code not in the RecyclerView Adapter code.
This is what worked for me:
recyclerView.setAdapter(new RecyclerViewAdapter(newList));
recyclerView.invalidate();
After creating a new adapter that contains the updated list (in my case it was a database converted into an ArrayList) and setting that as adapter, I tried recyclerView.invalidate()
and it worked.