title: What's wrong with my DiffUtil implementation?
body: <p><strong>Update</strong>: One of the problems is solved: Now <code>updateList</code> is resolved, the problem was that I defined <code>mAdapter</code> as <code>RecyclerView.Adapter</code> instead of <code>MyAdapter</code>. But now even though I am getting data, nothing shows up on the list, it's empty</p>
  
  <p>--------------------<strong>ORIGINAL POST</strong>--------------------</p>
  
  <p>I want to update my <code>RecyclerView</code> using <code>DiffUtil</code> to prevent duplicates. </p>
  
  <p>I have 4 classes: The User class, the <code>Activity</code> class where I set data, the <code>Adapter</code> class and the <code>DiffUtil</code> class. I am not sure I combine all these 4 correctly.</p>
  
  <p>This is the User class:</p>
  
  <pre><code>public class User {
  
      private String mUserId;
      private Uri mImageUrl;
  
  
      public User(String userId, String imageUrl) {
          mUserId = userId;
          mImageUrl = Uri.parse(imageUrl);
      }
  
  
      public String getUserId() {
          return mUserId;
      }
  
      public Uri getImageUrl() {
          return mImageUrl;
      }
  }
  </code></pre>
  
  <p>This is how I set data dynamically (I keep getting new <code>Json</code> arrays from the server containing user id's to be displayed, then I set the user image from <code>Firebase</code> storage): (It's a function invoked by an onClick listener:)</p>
  
  <p>This is the method call from the fragment:</p>
  
  <pre><code>button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {               
          updateUsersList();
      }
  });
  </code></pre>
  
  <p>This is the function:</p>
  
  <pre><code>   private void updateUsersList() {
          @Override
          public void onResponse(JSONArray response) { // the JSON ARRAY response of user ids ["uid1", "uid334", "uid1123"]
              myDataset.clear(); // clear dataset to prevent duplicates
              for (int i = 0; i &lt; response.length(); i++) {
                  try {
                      String userKey = response.get(i).toString(); // the currently iterated user id
                      final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                      DatabaseReference userKeyRef = rootRef.child("users").child(userKey); // reference to currently iterated user
                      ValueEventListener listener = new ValueEventListener() {
                      @Override
                      public void onDataChange(DataSnapshot dataSnapshot) {
                          myDataset.add(new User(dataSnapshot.getKey(), dataSnapshot.child("imageUrl").getValue().toString())); //add new user: id and image url
                          mAdapter.updateList(myDataset); // cannot resolve this method, why?
                     }
                     @Override
                     public void onCancelled(@NonNull DatabaseError databaseError) {
                     Log.d(TAG, databaseError.getMessage());
                     }
                    };
                    userKeyRef.addListenerForSingleValueEvent(listener);
                }
                catch (JSONException e) { Log.d(TAG, "message " + e); }
             }
     }
  </code></pre>
  
  <p>This is how my <code>DiffUtil</code> class looks like:</p>
  
  <pre><code>public class MyDiffUtilCallBack extends DiffUtil.Callback{
  
      ArrayList&lt;User&gt; oldUsers;
      ArrayList&lt;User&gt; newUsers;
  
      public MyDiffUtilCallBack(ArrayList&lt;User&gt; newUsers, ArrayList&lt;User&gt; oldUsers) {
          this.newUsers = newUsers;
          this.oldUsers = oldUsers;
      }
  
      @Override
      public int getOldListSize() {
          return oldUsers.size();
      }
  
      @Override
      public int getNewListSize() {
          return newUsers.size();
      }
  
      @Override
      public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
          return oldUsers.get(oldItemPosition).getUserId().equals( newUsers.get(newItemPosition).getUserId());
      }
  
      @Override
      public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
          return oldUsers.get(oldItemPosition).equals(newUsers.get(newItemPosition));
      }
  
      @Nullable
      @Override
      public Object getChangePayload(int oldItemPosition, int newItemPosition) {
          //you can return particular field for changed item.
          return super.getChangePayload(oldItemPosition, newItemPosition);
      }
  }
  </code></pre>
  
  <p>And this is my adapter:</p>
  
  <pre><code>public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.MyViewHolder&gt; {
  
      private ArrayList&lt;User&gt; mDataset;
      private MyViewHolder myHolder;
      private User user;
  
      public static class MyViewHolder extends RecyclerView.ViewHolder {
  
          public TextView singleItemTextView;
          public ImageView singleItemImage;
          public View layout;
          public ConstraintLayout constraintLayout;
          public MyViewHolder(View v) {
              super(v);
              layout = v;
              singleItemImage = (ImageView) v.findViewById(R.id.icon);
              singleItemTextView = (TextView) v.findViewById(R.id.singleitemtv);
              constraintLayout = (ConstraintLayout) v.findViewById(R.id.nbConstraintLayout);
          }
      }
  
      // Provide a suitable constructor (depends on the kind of dataset)
      public MyAdapter(ArrayList&lt;User&gt; myDataset) {
          mDataset = myDataset;
      }
  
      // Create new views (invoked by the layout manager)
      @Override
      public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
  
  
          View v =  LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.nb_image_view, parent, false);
  
          MyViewHolder vh = new MyViewHolder(v);
          return vh;
      }
  
      @Override
      public void onBindViewHolder(final MyViewHolder holder, final int position) {
          myHolder = holder;
  
  
          user = mDataset.get(position);
          Uri userImage = user.getImageUrl();       
          myHolder.singleItemTextView.setText(user.getUserId());
  
          Glide.with(myHolder.itemView.getContext() /* context */)
                  .load(userImage)
                  .into(myHolder.singleItemImage);
          myHolder.constraintLayout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {               
                   Context context = v.getContext();                
                  Intent intent = new Intent(v.getContext(), DisplayUserActivity.class);
                context.startActivity(intent);
              }
          });
  
      }
      public void updateList(ArrayList&lt;User&gt; newList) {
          DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDiffUtilCallBack(this.mDataset, newList));
          diffResult.dispatchUpdatesTo(this);
      }
  }
  </code></pre>
  
  <p>I am not sure I combine all the classes correctly (my first time using <code>DiffUtil</code>), and I also get <code>cannot resolve method updateList(?)</code></p>
  
  <p>What am I doing wrong? </p>
  
  <p>This is how I define mAdapter in my Fragment:</p>
  
  <pre><code>public class MyFragment extends Fragment {
      private ArrayList&lt;User&gt; myDataset;
      private RecyclerView.Adapter mAdapter;
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          // Inflate the layout for this fragment
          rootView = inflater.inflate(R.layout.fragment_lks, container, false);
  
          mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
          myDataset = new ArrayList&lt;User&gt;();
          mAdapter = new MyAdapter(myDataset);
  </code></pre>