Set notifyDataSetChanged() on Recyclerview adapter

不羁的心 提交于 2019-12-01 03:57:09

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:

public void setItems(List<ServiceModel> persons) {
    this.persons = persons;
}

and call it before notifyDataSetChanged() like this:

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

Issue is in these lines..

     adapter = new RVAdapter(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.

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.

when you every fill your list call below method.

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

}

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

Simply in RVAdapter:

public void refreshList(){
    notifyDataSetChanged();
}

and call this method in MainActivity when need:

rVAdapter.refreshList();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!