0

I have recycle-view with custom data and each recycle-view item has a checkbox in it. the problem is when I check the checkbox of first item the last items checkbox get selected automatically I have tried few things but those are not working

Can someone help me with this Here is my adapter code

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private Context context;
    private List<Upload> uploads;
    private static ArrayList<Upload> uploadList = new ArrayList<>();
    private FirebaseStorage mStorage;
    private DatabaseReference mDatabase;
    InvoiceRepository invoiceRepository;
    OnRecycleClickListener recycleClickListener;

    public MyAdapter(Context context, List<Upload> uploads, OnRecycleClickListener onRecycleClickListener) {
        this.uploads = uploads;
        this.context = context;
        this.recycleClickListener = onRecycleClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_images, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Upload upload = uploads.get(position);


        //  holder.textViewName.setText(upload.getName());
        holder.textViewdesc.setText(upload.getDesc());

        Glide.with(context).load(upload.getUrl()).placeholder(R.drawable.loading).into(holder.imageView);

        holder.imagecard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                final Dialog dialog = new Dialog(holder.imagecard.getContext());
                dialog.setContentView(R.layout.customdialogboximagedisplay);
                dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

                final ImageView image = (ImageView) dialog.findViewById(R.id.floorimage);
                Glide.with(holder.imagecard.getContext())
                        .load(upload.getUrl())
                        .placeholder(R.drawable.loading)
                        .into(image);

                dialog.show();
                Window window = dialog.getWindow();
                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, 
    ViewGroup.LayoutParams.WRAP_CONTENT);

            }
        });

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    uploadList.add(upload);
                    recycleClickListener.onRecycleClick(uploadList);
                } else if (!isChecked) {
                    uploadList.remove(position);
                    recycleClickListener.onRecycleClick(uploadList);
                }
            }
        });
   }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        //  public TextView textViewName;
        public EditText textViewdesc;
        public ImageView imageView;
        public CardView imagecard;
        public CheckBox checkBox;
        public Button Edit;


        public ViewHolder(View itemView) {
            super(itemView);

            //  textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewdesc = (EditText) itemView.findViewById(R.id.textViewdescription);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            imagecard = (CardView) itemView.findViewById(R.id.cardimage);
            checkBox = itemView.findViewById(R.id.checkbox1);
            Edit = (Button) itemView.findViewById(R.id.Edit_desc);
        }
    }
}
MMG
  • 3,226
  • 5
  • 16
  • 43

3 Answers3

0

You need mark checkboxes at every onBindViewHolder. To prevent OnCheckedChangeListener listeners from double triggering, add custom method to mark the checkbox in it's class. See my answer how to do this https://stackoverflow.com/a/45733496/5279156

Vlad
  • 7,997
  • 3
  • 56
  • 43
0

Try to make use of

setItemViewCacheSize() in your activity.

Please check if this answer might help you: Duplication in Checkbox selection in RecyclerView

Android_id
  • 1,521
  • 1
  • 15
  • 33
-1

Please add below line in your adapter.

        if(uploads.contains(upload))
          mCheck.setChecked (true);
       else
          mCheck.setChecked (false);

Add change your setOnCheckedChangeListener like this way

 holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(!buttonView.isPressed()) {
                      return;
                      }
                    if (isChecked) {
                        uploadList.add(upload);
                        recycleClickListener.onRecycleClick(uploadList);
                    } else if (!isChecked) {
                        uploadList.remove(position);
                        recycleClickListener.onRecycleClick(uploadList);
                    }
                }
            });
Parth
  • 791
  • 7
  • 17
  • can you clarify what is mListener – Sagar Deshmukh Apr 08 '20 at 04:57
  • try adding this line in your `onbindViewHolder` -> `viewHolder.setIsRecyclable(false);` although i won't recommend this as it'll stop recycling your View anymore and will works as list view. – Parth Apr 08 '20 at 06:29