1

Suppose I have a recyclerView Adapter which is used to populate views including imageView . When i first load adapter then recycler view items are getting displayed properly . But if i update imageView and textview resource in adapter datasource and call notifyDataSetChanged() method then only textview is getting updated and not imageView . Can anyone help me with this ? Thanks in advance .

For Example - This is the first time i add all the elements in an empty arraylist inside fragment and update using notifyDataSetChanged() method . Till now everything is fine and images are loading correctly in adapter .

Below is the getMethod()

                    if(remindersListResponse.code == 200)
                    {
                        if(remindersListResponse.data != null)
                        {
                            photoContactList.clear();

                           photoContactList.addAll(remindersListResponse.data);
                            photoContactsAdapter.notifyDataSetChanged();
                        }

                    }

Now if i call getMethod again on click of some button in fragment and use notifyDataSetChanged() method . Then only text gets updated in adapter items but not images instead they get disappear .

This is my adapter onBindViewHolder(...) method

@Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {

        final PhotoContactsResponse.Result photoContactDetail = photoContactList.get(i);

        viewHolder.photoContactName.setText(photoContactDetail.name);

        if(photoContactDetail.image!=null && !photoContactDetail.image.isEmpty()) {
            viewHolder.addPhotoText.setVisibility(View.GONE);
            Picasso.with(context)
                    .load(Constants.WEB_SERVICE_BASE_URL_FOR_IMAGE+photoContactDetail.image)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(viewHolder.photoContactImage);
        }
        else
        {
            viewHolder.addPhotoText.setVisibility(View.VISIBLE);
            viewHolder.photoContactImage.setVisibility(View.GONE);
        }
}

This is my model class

public static class Result{
        public int id;
        public String name;
        public String number;
        public String image;
        public int index;
    }
Rishabh
  • 11
  • 3

2 Answers2

0

I was running into this same issue and fixed it by setting the recycle view's adapter to null, then to my adapter again. See: Force RecyclerView to redraw its items

  • Welcome to Stack Overflow! I wouldn't recommend this approach, this is just a workaround that doesn't fix the OP's mistake. I think the answer a couple of years ago gives a working solution even though the OP didn't accept it. – Henry Twist Mar 06 '21 at 01:17
  • 1
    Got it, thanks for the info. I am admittedly new to android development so I was just happy to get it working – Jadyn Brown Mar 08 '21 at 19:37
0

I think you forgot to set ImageView as VISIBLE. When you hide it in else statement you should always bring it back when image is available. onBindViewHolder only binds data to view. Recycler views are reused between calls to onBindViewHolder method.

@Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {

        final PhotoContactsResponse.Result photoContactDetail = photoContactList.get(i);

        viewHolder.photoContactName.setText(photoContactDetail.name);

        if(photoContactDetail.image!=null && !photoContactDetail.image.isEmpty()) {
            viewHolder.addPhotoText.setVisibility(View.GONE); 
            viewHolder.photoContactImage.setVisibility(View.VISIBLE); // <== ADD THIS LINE

            Picasso.with(context)
                    .load(Constants.WEB_SERVICE_BASE_URL_FOR_IMAGE+photoContactDetail.image)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(viewHolder.photoContactImage);
        }
        else
        {
            viewHolder.addPhotoText.setVisibility(View.VISIBLE);
            viewHolder.photoContactImage.setVisibility(View.GONE);
        }
}
mdev
  • 534
  • 1
  • 6
  • 14