32

I extended

RecyclerView.Adapter<RecyclerView.ViewHolder>

And when I called:

mRecyclerView.getAdapter().notifyDataSetChanged();

Nothing happened.

The only way to refresh the view is to set again the adapter (see this answer):

mRecyclerView.setAdapter(new MyAdapter(...));

I have two issues with this solution:

  1. I can see a blink on the screen when I set again the adapter
  2. The listview returns to the first position.

Any ideas?

Community
  • 1
  • 1
David
  • 37,109
  • 32
  • 120
  • 141
  • Have you tried to encapsulate notifyDataSetChanged method in MyAdapter? Eg: Create "myNotifyDataSetChanged" in MyAdapter and from that method call this.notifyDataSetChanged()". Save MyAdapter reference and call myAdapterRef.myNotifyDataSetChanged() instead of myRecyiclerView.getAdapter().notifyDataSetChanged(). Sont know why it worked for me (i had that problem with ListView) – bajicdusko Dec 30 '14 at 23:39
  • @helionprime Didn't help :( – David Dec 31 '14 at 00:01
  • ok, my bad. I forgot to get/use the updated Data within the `onBindViewHolder` method. But what about the blink effect, how can I get rid of it? – David Dec 31 '14 at 00:44
  • ok, my bad2. The blink effect related to load image into the imageViews within my items. It's a known issue of UniversalImageLoader (https://github.com/nostra13/Android-Universal-Image-Loader/issues/376) that I'm using. – David Dec 31 '14 at 02:22
  • I am glad you've managed to solve it :-). If you want to try to get rid of fliickering, on this link https://github.com/bajicdusko/AndroidJsonProvider/tree/master/app/src/main/java/com/bajicdusko/ajp/tools you can try Image loader i have developed for myself. It also supports caching, optimizing, autodownload and showing default image in case of errors. – bajicdusko Dec 31 '14 at 06:31
  • 2
    Were u able to find the solution of your problem@David. I have the same problem. – Gaurav Arora Feb 02 '16 at 09:28

8 Answers8

37

If notifyDataSetChanged() does not trigger view updates than there is a chance that you have forgotten to call SetLayoutManager() on your RecyclerView (like I did!). Just don't forget to do this: Java code:

LinearLayoutManager layoutManager = new LinearLayoutManager(context ,LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager)

C# code, I'm using Xamarin.

var layoutManager = new LinearLayoutManager(Context, LinearLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutManager);

before you call recyclerView.SetAdapter(adapter);

If you prefer declaring it in xml, so you cannot forget it in the code you can also add these lines to the xml of your recyclerView:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"
Dbl
  • 5,634
  • 3
  • 41
  • 66
Varvara Kalinina
  • 2,043
  • 19
  • 29
28

If your getItemCount() returns 0, then notifyDataSetChanged() won't do anything. Make sure that when you initialize your adapter, you are passing a valid dataset.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
4

According to the javadocs: If you are writing an adapter it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort.

public class NewsAdapter extends RecyclerView.Adapter<...> {    

private static List mFeedsList;
...

    public void swap(List list){
    if (mFeedsList != null) {
        mFeedsList.clear();
        mFeedsList.addAll(list);
    }
    else {
        mFeedsList = list;
    }
    notifyDataSetChanged();
}

I am using Retrofit to fetch the list, on Retrofit's onResponse() use,

adapter.swap(feedList);
Akshay Goyal
  • 941
  • 9
  • 11
  • "According to the javadocs: If you are writing an adapter it will always be more efficient to use the more specific change events if you can" where in javadocs did you find this info? – Marian Paździoch Oct 12 '16 at 13:58
  • https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged() – Akshay Goyal Oct 13 '16 at 06:56
4

To update recyclerview we can do the following:

  1. Create and set adapter again:

    adapter=new MyAdapter(...);
    mRecyclerView.setAdapter(adapter);
    
  2. Clear model list data and then notify:

    List<YourModel> tempModel=new ArrayList<>(modelList); 
    modelList.clear();   
    modelList.addAll(tempModel); 
    adapter.notifyDataSetChanged();
    
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Mayur Prasad
  • 752
  • 5
  • 7
  • There is no need to create a new adapter every time. All you need is to pass a new data to the existing adapter. Please, see my working example below. – akelec Oct 15 '19 at 13:33
4

In my case, nothing but this has worked. When it's time to refresh a RecyclerView, I did:

array = getNewItems();                    // populates the list with new items
((MyAdapter) mAdapter).setValues(array);  // pass the new list to adapter !!!
mAdapter.notifyDataSetChanged();          // tell the adapter a data set has changed

In the MyAdapter, I put the setter for data:

public void setValues(List<Item> items){
    this.items = items;
}

That's it!

[Notice that the notifyDataSetChanged does not work without setting new values to adapter.]

akelec
  • 3,797
  • 3
  • 41
  • 39
0

Want to share something, I was facing the same issue. But what i was doing wrong was. I was creating instance of Adapter every time new and than doing notifysetDatachange() to that new instance not the older one.

So please make sure Adapter whom you notifysetDatachange() should be older one. Hope below example helps..

     MyAdapter mAdapter = new MyAdapter(...)

    mRecyclerView.setAdapter(mAdapter );

// TODO 
mAdapter.modifyData(....);
mAdapter.notifySetDataChange();


    MyAdapter extends baseAdapter {
     MyAdapter () {
    }
    modifyData(String[] listData) {

    }

    }
sharma_kunal
  • 2,152
  • 1
  • 28
  • 28
0

notifyDataSetChanged() sholud be called in main thread.

inhogo
  • 253
  • 3
  • 9
0

So i fixed my problem like this : orderList is the List that i pass on to the recyclerview . We can just add the item at the position in the list, here 0 is the 0th position in the List . Then call adapter.notifyDataSetChanged() . Works like a charm

1) orderList.add(0,String); 2) orderAdapter.notifyDataSetChanged();