Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I update RecyclerView in real time?

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 ?

like image 788
Ali Ahmed Avatar asked Oct 14 '16 09:10

Ali Ahmed


2 Answers

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();
        }
    }

}
like image 150
Mohammed Rampurawala Avatar answered Oct 24 '22 08:10

Mohammed Rampurawala


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.

like image 34
SpiralDev Avatar answered Oct 24 '22 09:10

SpiralDev