Cannot resolve adapter method when using DifUtil

47
January 12, 2019, at 7:00 PM

I want to update my RecyclerView using DifUtil to prevent duplicates. This is how my User class look like:

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

This is how I set data dynamically (I keep getting new Json arrays from the server containing user id's to be displayed, then I set the user image from Firebase storage):

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

This is how my DifUtil class looks like:

public class MyDiffUtilCallBack extends DiffUtil.Callback{
    ArrayList<User> oldUsers;
    ArrayList<User> newUsers;
    public MyDiffUtilCallBack(ArrayList<User> newUsers, ArrayList<User> 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() == 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);
    }
}

And this is my adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private ArrayList<User> 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<User> 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<User> newList) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDiffUtilCallBack(this.mDataset, newList));
        diffResult.dispatchUpdatesTo(this);
    }
}

I am not sure I combine all the classes correctly (my first time using DifUtil), and I also get cannot resolve method updateList(?)

What am I doing wrong?

READ ALSO
Sorting of ArrayList shows first item twice

Sorting of ArrayList shows first item twice

i try to Sort my arrayList depending on the distance between user and the locationTo do this i created the following two methods:

33
How to customize SwipeToReferesh layout top ProgressBar or Loader?

How to customize SwipeToReferesh layout top ProgressBar or Loader?

I would like to know how can I customize SwipeToRefresh layoutIn specific, I am trying to hide that loader

19
How to solve dlopen failed: library &ldquo;../../lib/libtbb.so&rdquo; not found error?

How to solve dlopen failed: library “../../lib/libtbb.so” not found error?

I am working on android app with Face Recognization using OpenCvI found this repo here's a link

11
Dagger 2 get old token when token is refreshed

Dagger 2 get old token when token is refreshed

I am having a problem with dagger 2Briefly, dagger 2 makes the injection in my fragment, and if the token is expired

10