1

I am using Recycler View for creating a list of items and I am getting a duplicate items in the list. I have passed a list of 30 size into the Recycler View Adapter. The created list has 30 items but there are only 3 unique items, all other are repetition of 3 unique items. I am not able to find the bug.

public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.CollectionViewHolder> {
private List<CollectionDataTypeModel> mDataSet = new ArrayList<CollectionDataTypeModel>();
private Activity mActivity;
private String mType;


public CollectionAdapter(List<CollectionDataTypeModel> mDataSet, Activity activity, String type) {
    this.mActivity = activity;
    this.mDataSet = mDataSet;
    this.mType = type;

}

@Override
public CollectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection_cardview_layout, parent, false);
    CollectionViewHolder vh = new CollectionViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(final CollectionViewHolder holder, int position) {


    final CollectionDataTypeModel collectionData = mDataSet.get(position);
    Log.d("Colelction Adapter","name : "+collectionData.getCollectionName());
    holder.titleText.setText(collectionData.getCollectionName()+"");
    holder.secondaryText.setText(collectionData.getPoiCount()+" attractions");
    if (mType.equalsIgnoreCase("Collection")) {
        Glide.with(mActivity).load(R.drawable.aman).asBitmap().centerCrop().into(new BitmapImageViewTarget(holder.profileImage) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(mActivity.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                holder.profileImage.setImageDrawable(circularBitmapDrawable);
            }
        });
    } else if (mType.equalsIgnoreCase("Destination")) {
        holder.profileImageRipple.setVisibility(View.GONE);
    } else if (mType.equalsIgnoreCase("Download")) {
        holder.profileImageRipple.setVisibility(View.GONE);
        holder.viewIcon.setImageResource(R.drawable.clear_icon);
        holder.viewCount.setVisibility(View.INVISIBLE);
    }
     //    holder.collectionImage.setImageResource(R.drawable.goldentemple);
        Glide.with(mActivity).load(collectionData.getCollectionImage()).into(holder.collectionImage);

    holder.likeCount.setText(Integer.toString(collectionData.getLikeCount()));
    holder.viewCount.setText(Integer.toString(collectionData.getViewCount()));

    holder.likeIconRipple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
        @Override
        public void onComplete(RippleView rippleView) {
           // if(holder.likeIcon.getDrawable()==mActivity.getDrawable(R.drawable.like_icon))
           {
                holder.likeIcon.setImageResource(R.drawable.like_fill_icon);
            }
           // else
            {
             //   holder.likeIcon.setImageResource(R.drawable.like_icon);
            }
        }
    });
    holder.collectionImageRipple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
        @Override
        public void onComplete(RippleView rippleView) {
            EventBus.getDefault().post(new CollectionMessageEvent(collectionData));
        }
    });


}

@Override
public int getItemCount() {
    return mDataSet.size();
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

public static class CollectionViewHolder extends RecyclerView.ViewHolder {
    TextView titleText;
    ImageView collectionImage;
    TextView secondaryText;
    TextView likeCount;
    TextView viewCount;
    ImageView profileImage;
    ImageView viewIcon;
    RippleView collectionImageRipple;
    RippleView profileImageRipple;
    RippleView likeIconRipple;
    ImageView likeIcon;


    public CollectionViewHolder(View itemView) {
        super(itemView);
        // View bottombar =  findById(itemView,R.id.bottomBar);

        collectionImage = findById(itemView, R.id.collection_image);
        collectionImageRipple = findById(itemView, R.id.collection_image_ripple);
        profileImage = findById(itemView, R.id.profileimage);
        profileImageRipple = findById(itemView, R.id.profileimage_ripple);
        titleText = findById(itemView, R.id.title_text);
        secondaryText = findById(itemView, R.id.secondary_text);
        likeCount = findById(itemView, R.id.like_count);
        likeIcon = findById(itemView, R.id.like_icon);
        likeIconRipple = findById(itemView, R.id.like_icon_ripple);
        viewCount = findById(itemView, R.id.view_count);
        viewIcon = findById(itemView, R.id.view_icon);
    }


}


}
Aman Jain
  • 2,975
  • 1
  • 20
  • 35
  • Actually it is quite trivial to help you on this issue. My suggestion is, attach a debugger on the process and check the MemoryAddress of the Lists that you are passing to the Adapter's constructor. You might be doing some work on the lists, outside of the Adapter's scope, but as long as they point to the same MemoryAddress they get modified as well. – Pavlos Apr 08 '16 at 10:44
  • I haven't doing any work on the List that I have passed in the Adapter Constructor.The OnBindViewHolder method of Adapter is calling only thrice instead of 30 times and inside this method I have checked out the List Size and it is 30. – Aman Jain Apr 08 '16 at 11:13
  • Currently I am getting data from json and then parse into a class object of ItemModel. When I was using the static json that I had put in the asset folder at that time there was no duplicate issue but when I getting json from network it started creating duplicate. I don't think so it has any effect due to the static json or getting from network. – Aman Jain Apr 08 '16 at 11:17
  • 1
    Issue has been resolved ,it was due to the use of setHasStableId(true) in recycler view. – Aman Jain Apr 08 '16 at 12:24

0 Answers0