RecyclerView adapter notifyDataSetChanges() not refreshing recyclerview

261 views Asked by At

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?

4

There are 4 answers

0
klcmin On
    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();
3
Elias N On

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

0
santosh kumar On
@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();

}
1
savepopulation On

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