notifyDataSetChange not working on RecyclerView

44,607

Solution 1

My issue was that I was not notifying the change on the main thread, therefore the change was not visible right away. It is the same issue pointed out here.

Solution 2

I was trying to update RecycleView with notifyDataSetChanged() method in response to com.google.common.eventbus.Subscribe.

Like @wmora mentioned the problem was that the notify method was not called in the main UI thread.

I resolved it with AndroidAnnotations' @UiThread

@UiThread
protected void dataSetChanged() {
    notifyDataSetChanged();
}

which is equivalent to:

 final Adapter adapter = this;
 new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
       adapter.notifyDataSetChanged();
    }
 });

note: just separate new Handler into class private field

Share:
44,607

Related videos on Youtube

wmora
Author by

wmora

Software developer (duh!)

Updated on July 09, 2022

Comments

  • wmora
    wmora almost 2 years

    I'm working with Android's new RecyclerView but I can't get my custom adapter to refresh whenever I call one of the "notify" methods.

    I've tried calling notifyDataSetChanged, notifyItemRangeInserted and notifyItemInserted and none of them seem to work.

    Here's the code for my custom adapter. I'm basically trying to refresh a list of Strings:

    package com.mycompany.myapp.adapters;
    
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import com.mycompany.myapp.R;
    
    import java.util.List;
    
    public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
    
        private List<String> mDataset;
    
        public FeedAdapter(List<String> dataset) {
            super();
            mDataset = dataset;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
            LinearLayout v = (LinearLayout) LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item_feed, parent, false);
            v.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
        return new ViewHolder(v);
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.setText(mDataset.get(position));
        }
    
        @Override
        public int getItemCount() {
            return mDataset.size();
        }
    
        public void setDataset(List<Status> dataset) {
            mDataset = dataset;
            // This isn't working
            notifyItemRangeInserted(0, mDataset.size());
        }
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
            private TextView mFeedText;
    
            public ViewHolder(View v) {
                super(v);
                mFeedText = (TextView) v.findViewById(R.id.feed_text);
            }
    
            private void setText(String text) {
                mFeedText.setText(text);
            }
        }
    }
    

    Anyone else having this issue?

    Thanks!

    • nhaarman
      nhaarman almost 10 years
      Could you show your RecyclerView and Adapter assignments?
  • Boy
    Boy over 9 years
    This is about RecyclerView
  • prakashpun
    prakashpun over 8 years
    I am facing an issue regarding RecyclerView. When i add item , it doesnt show up in the list. When i rotate the screen, since the page is recreated the newly added item is displayed. What i do is, when i click add button i add the item and call adapter.notifyDataSetChanged();
  • Rohit Mandiwal
    Rohit Mandiwal over 7 years
    what an observation! you saved my day.
  • momvart
    momvart almost 7 years
    Thanks. My problem was that I changed an imageview from another thread.
  • Jeffrey Blattman
    Jeffrey Blattman over 4 years
    That annotation is a marker only used for code correctness. It is not equivalent to posting to a handler.