mardi 7 avril 2020

Recycle view first and last items getting selected when i select only the first one

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



Aucun commentaire:

Enregistrer un commentaire