0

I have set up a RecyclerView like this

@Override public void setTenData(List<Data> dataList) { Timber.d("set ten drugs size %s",dataList.size()); this.dataList = dataList; dataListAdapter = new DataListingAdapter(getActivity(), this.dataList); mRecyclerView.setAdapter(dataListAdapter); } 

And in another method I get new data and when I try to refresh the RecyclerView like this

@Override public void setNewsData(List<Data> dataList) { this.dataList = dataList; Timber.d("size of news data %s",this.dataList.size()); dataListAdapter.notifyDataSetChanged(); } 

The RecyclerView doesn't get updated. What might be the problem?

3
  • set it to the adapter Commented Dec 29, 2016 at 14:29
  • it works that way, but shouldn't it be also working correctly the way I've done. Commented Dec 29, 2016 at 15:04
  • @theanilpaudel no, your way changes the reference in your view from the old list to the new list. Your adapter's reference still points to the old list. Commented Dec 29, 2016 at 15:19

4 Answers 4

2

You need to set your new data in your adapter, not just in your view

Sign up to request clarification or add additional context in comments.

3 Comments

You answer is totally correct, and with you permission I will add to be sure that notifiDataSetChanged was done on MainThread :)
this.dataList is set in the adapter.
Add a function in your adapter: public void setDataList(List<Data> dataList) and in it set the new data to the adapter's list. And as @Neonamu said, make sure that notifyDataSetChanged is called from the main thread.
0
@Override public void setTenData(List<Data> dataList) { Timber.d("set ten drugs size %s",dataList.size()); this.dataList = dataList; dataListAdapter = new DataListingAdapter(getActivity(), this.dataList); mRecyclerView.setAdapter(dataListAdapter); dataListAdapter.notifyDataSetChanged(); } 

Comments

0

You can simply clear your list and add new data to it and than notify adapter. Here i modified your code:

@Override public void setNewsData(List<Data> dataList) { this.dataList.clear(); this.dataList.addAll(dataList); dataListAdapter.notifyDataSetChanged(); } 

1 Comment

Data class there extends RealmObject so we can't clear and then addAll in this list.
0
 public void setNewsData(List<Data> dataList) { this.dataList.addAll(dataList); // if datalist has only new data //this.dataList = dataList;// if datalist has all data Timber.d("size of news data %s",this.dataList.size()); } 

And then call the main thread;

dataListAdapter = (RecyclerViewAdapter) recyclerView.getAdapter(); dataListAdapter.setNewsData(dataList); dataListAdapter.notifyDataSetChanged(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.