Skip to content Skip to sidebar Skip to footer

Set Notifydatasetchanged() On Recyclerview Adapter

I'm implementing an endless data loading for a RecyclerView. When software detects that last item is going to be shown, it downloads new items and call to the loadMoreData() functi

Solution 1:

you are setting the new list to the RecyclerView Adapter , set the list in the Adapter:

make a method setItems(list) in adapter and call it before notifyDataSetChanged() and in adapter do

this.persons = new ArrayList<>(persons);

in setItems

add this method in adapter:

publicvoidsetItems(List<ServiceModel> persons) {
    this.persons = persons;
}

and call it before notifyDataSetChanged() like this:

adapter.setItems(list);
adapter.notifyDataSetChanged();

Solution 2:

Issue is in these lines..

     adapter = newRVAdapter(RecyclerViewActivity.this, list);
        rv.setAdapter(adapter);
        adapter.notifyDataSetChanged();

You are initialising your adapter every time. No need to reinitialize it. Just update your arraylist and invoking to adapter.notifyDataSetChanged(); will make it work.

Solution 3:

Like @Beena mentioned, you are creating and setting new adapter ever time, in your success response.

One approach would be to create an adapter and set it to the recycler view only for the first time, and then onSuceess() of your api callback, call a method of your adapter.

In, them adapter method, just add that new data in your main arraylist and do notifyItemInserted() instead of notifyDataSetChanged, in this way you will also see the default adding animation of recyclerView.

Solution 4:

Every time you fill your list call the method below:

if (adapter != null) // it works second time and later
   adapter.notifyDataSetChanged();
else { // it works first time
  adapter = new AdapterClass(context,list);
  listView.setAdapter(adapter);
}

Solution 5:

I solved this with added method in RVAdapter that call notifyDataSetChanged().

Simply in RVAdapter:

publicvoidrefreshList(){
    notifyDataSetChanged();
}

and call this method in MainActivity when need:

rVAdapter.refreshList();

Post a Comment for "Set Notifydatasetchanged() On Recyclerview Adapter"