I have a list which can be updated any time by any user. The list is showed by in a RecyclerView. I want to update this list in real time so that user can get a better user experience because user need not to perform refresh several times. Now question is, how can I update this list in real time ?
With RecyclerView you can either update a single item, item range or the whole list with the adapter.
You have to use RecyclerView
adapter's method like adapter#notifyItem***
or adapter#notifyDataSetChanged()
(this will update the whole list of items in the RecyclerView).
class RVAdapter extends ...{
List<?> itemList;
public void updateList (List<Object> items) {
if (items != null && items.size() > 0) {
itemList.clear();
itemList.addAll(items);
notifyDataSetChanged();
}
}
}
You can create metod inside your Adapter which updates the data and refreshes ur recyclerview.
class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
List<Data> data;
...
public void update(ArrayList<Data> datas){
data.clear();
data.addAll(datas);
notifyDataSetChanged();
}
...
}
Just call the update method passing new data. That's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With