Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

recyclerView with multiple checkBoxes problem handling check boxes

I have a custom list view with some check box in each item I tried many ways to handle those check boxes but none of them work

when I check one of the check boxes for example in recycler views first item and thes scroll down and up I see the other check boxes in recycler views first item are checked and not just that also in recycler views other items some checkboxes are checked please help me its been two days that I working on this problem

my model class code:

public class DrugList {
    public String drugTittle;
    public String drugDoz1;
    public String drugDoz2;
    public String drugDoz3;
    public String drugDoz4;
    private boolean isChecked_drugDoz1;
    private boolean isChecked_drugDoz2;
    private boolean isChecked_drugDoz3;

    public String getdrugTittle() {
        return drugTittle;
    }

    public void setDrugTittle(String drugTittle) {
        this.drugTittle = drugTittle;
    }


    public String getdrugDoz1() {
        return drugDoz1;
    }

    public void setdrugDoz1(String drugDoz1) {
        this.drugDoz1 = drugDoz1;
    }
    public String getdrugDoz2() {
        return drugDoz2;
    }

    public void setdrugDoz2(String drugDoz2) {
        this.drugDoz2 = drugDoz2;
    }

    public String getdrugDoz3() {
        return drugDoz3;
    }

    public void setdrugDoz3(String drugDoz3) {
        this.drugDoz3 = drugDoz3;
    }


    public String getdrugDoz4() {
        return drugDoz4;
    }

    public void setdrugDoz4(String drugDoz4) {
        this.drugDoz4 = drugDoz4;
    }



    public boolean getisChecked_drugDoz1() {
        return isChecked_drugDoz1;
    }

    public void setisChecked_drugDoz1(boolean checked) {
        isChecked_drugDoz1 = checked;
    }


    public boolean getisChecked_drugDoz2() {
        return isChecked_drugDoz2;
    }

    public void setisChecked_drugDoz2(boolean checked) {
        isChecked_drugDoz2 = checked;
    }

    public boolean getisChecked_drugDoz3() {
        return isChecked_drugDoz3;
    }

    public void setisChecked_drugDoz3(boolean checked) {
        isChecked_drugDoz3 = checked;
    }
    public boolean getisChecked_noChildItem() {
        return isChecked_noChildItem;
    }

    public void setisChecked_noChildItem(boolean checked) {
        isChecked_noChildItem = checked;
    }
}

and my adpter class is:

public class DrugListAdapter extends RecyclerView.Adapter<DrugListAdapter.DrugsViewHolder> {

    public static ArrayList<String> selectedDrugsList = new ArrayList<String>();


    private HashMap<Integer, String> wichChecked = new HashMap<>();

    private ArrayList<DrugList> data;
    Context mContext;
    protected LayoutInflater vi;
    private Boolean[] checkBoxState = null;
    private HashMap<DrugList, Boolean> checkForDrugList = new HashMap<>();

    public DrugListAdapter(Context context, ArrayList<DrugList> object) {


        this.data = object;
        this.mContext = context;

    }

    @NonNull
    @Override
    public DrugsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {


        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.lyt_drugs_lv, viewGroup, false);

        return new DrugsViewHolder(view);


    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public void onBindViewHolder(@NonNull final DrugsViewHolder drugsViewHolder, int i) {

        DrugList drugList = data.get(i);
        drugsViewHolder.noChildItemChbox.setVisibility(View.GONE);
        drugsViewHolder.lyt_expand_input.setVisibility(View.GONE);
        drugsViewHolder.drugsTitle.setText(drugList.getdrugTittle());
        drugsViewHolder.btToggleInput.setVisibility(View.VISIBLE);

        drugsViewHolder.bind(i);


        if (drugList.getdrugDoz1() == "") {

            drugsViewHolder.btToggleInput.setVisibility(View.GONE);
            drugsViewHolder.noChildItemChbox.setVisibility(View.VISIBLE);
            drugsViewHolder.lyt_expand_input.setVisibility(View.GONE);

        } else {
            drugsViewHolder.chBoxDoz2.setText(drugList.getdrugDoz2());
            drugsViewHolder.chBoxDoz1.setText(drugList.getdrugDoz1());

        }


        if (drugList.getdrugDoz3() == "") {

            drugsViewHolder.chBoxDoz3.setVisibility(View.GONE);


        } else {
            drugsViewHolder.chBoxDoz3.setText(drugList.getdrugDoz3());
        }

        if (drugList.getdrugDoz4() == "") {
            drugsViewHolder.chBoxDoz4.setVisibility(View.GONE);

        } else {
            drugsViewHolder.chBoxDoz4.setText(drugList.getdrugDoz4());

        }


    }

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

    public class DrugsViewHolder extends RecyclerView.ViewHolder {
        final TextView drugsTitle;
        final CheckBox chBoxDoz1;
        final CheckBox chBoxDoz2;
        final CheckBox chBoxDoz3;
        final CheckBox chBoxDoz4;
        final CheckBox noChildItemChbox;
        final ImageButton btToggleInput;
        final LinearLayout lyt_expand_input;


        public DrugsViewHolder(@NonNull View itemView) {
            super(itemView);
            drugsTitle = itemView.findViewById(R.id.drug_tittle);
            chBoxDoz1 = itemView.findViewById(R.id.chBox_doz1);
            chBoxDoz2 = itemView.findViewById(R.id.chBox_doz2);
            chBoxDoz3 = itemView.findViewById(R.id.chBox_doz3);
            chBoxDoz4 = itemView.findViewById(R.id.chBox_doz4);
            noChildItemChbox = itemView.findViewById(R.id.no_child_item_chBox);
            btToggleInput = itemView.findViewById(R.id.bt_toggle_input);
            lyt_expand_input = itemView.findViewById(R.id.lyt_expand_input);


            final boolean[] inVis = {true};

            btToggleInput.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (inVis[0]) {
                        lyt_expand_input.setVisibility(View.VISIBLE);
                        inVis[0] = false;
                        btToggleInput.setImageResource(R.drawable.ic_arrow_up);
                    } else if (!inVis[0]) {
                        lyt_expand_input.setVisibility(View.GONE);
                        inVis[0] = true;
                        btToggleInput.setImageResource(R.drawable.ic_expand_arrow);

                    }
                }
            });

            chBoxDoz1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {


                    int adapterPosition = getAdapterPosition();
                    if (data.get(adapterPosition).getisChecked_drugDoz1()) {
                        chBoxDoz1.setChecked(false);
                        data.get(adapterPosition).setisChecked_drugDoz1(false);
                    } else {
                        chBoxDoz1.setChecked(true);
                        data.get(adapterPosition).setisChecked_drugDoz1(true);
                    }


                    if (chBoxDoz1.isChecked()) {

                        String drugName = (String) drugsTitle.getText() + chBoxDoz1.getText();
                        selectedDrugsList.add(drugName);
                        Toast.makeText(G.context, drugName + " " + "اضافه شد", Toast.LENGTH_SHORT).show();
                    } else {

                        for (int i = 0; i < selectedDrugsList.size(); i++) {
                            String drugName = (String) drugsTitle.getText() + chBoxDoz1.getText();
                            if (selectedDrugsList.get(i).contains(chBoxDoz1.getText()) && selectedDrugsList.get(i).contains(drugsTitle.getText())) {
                                selectedDrugsList.remove(i);
                                Toast.makeText(G.context, drugName + " " + "حذف شد", Toast.LENGTH_SHORT).show();
                            }

                        }
                    }

                }
            });
            chBoxDoz2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    int adapterPosition = getAdapterPosition();
                    if (data.get(adapterPosition).getisChecked_drugDoz2()) {
                        chBoxDoz2.setChecked(false);
                        data.get(adapterPosition).setisChecked_drugDoz2(false);
                    } else {
                        chBoxDoz2.setChecked(true);
                        data.get(adapterPosition).setisChecked_drugDoz2(true);
                    }


                    if (chBoxDoz2.isChecked()) {
                        String drugName = (String) drugsTitle.getText() + chBoxDoz2.getText();
                        selectedDrugsList.add(drugName);
                        Toast.makeText(G.context, drugName + " " + "اضافه شد", Toast.LENGTH_SHORT).show();
                    } else {
                        for (int i = 0; i < selectedDrugsList.size(); i++) {
                            String drugName = (String) drugsTitle.getText() + chBoxDoz2.getText();

                            if (selectedDrugsList.get(i).contains(chBoxDoz1.getText()) && selectedDrugsList.get(i).contains(drugsTitle.getText())) {
                                selectedDrugsList.remove(i);
                                Toast.makeText(G.context, drugName + " " + "حذف شد", Toast.LENGTH_SHORT).show();
                            }

                        }
                    }

                }
            });
            chBoxDoz3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {


                    int adapterPosition = getAdapterPosition();
                    if (data.get(adapterPosition).getisChecked_drugDoz3()) {
                        chBoxDoz3.setChecked(false);
                        data.get(adapterPosition).setisChecked_drugDoz3(false);
                    } else {
                        chBoxDoz3.setChecked(true);
                        data.get(adapterPosition).setisChecked_drugDoz3(true);
                    }

                    if (chBoxDoz3.isChecked()) {
                        String drugName = (String) drugsTitle.getText() + chBoxDoz3.getText();
                        selectedDrugsList.add(drugName);
                        Toast.makeText(G.context, drugName + " " + "اضافه شد", Toast.LENGTH_SHORT).show();
                    } else {

                        for (int i = 0; i < selectedDrugsList.size(); i++) {
                            String drugName = (String) drugsTitle.getText() + chBoxDoz3.getText();

                            if (selectedDrugsList.get(i).contains(chBoxDoz3.getText()) && selectedDrugsList.get(i).contains(drugsTitle.getText())) {
                                selectedDrugsList.remove(i);
                                Toast.makeText(G.context, drugName + " " + "حذف شد", Toast.LENGTH_SHORT).show();
                            }

                        }
                    }

                }
            });

            noChildItemChbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    int adapterPosition = getAdapterPosition();
                    if (data.get(adapterPosition).getisChecked_noChildItem()) {
                        noChildItemChbox.setChecked(false);
                        data.get(adapterPosition).setisChecked_noChildItem(false);
                    } else {
                        noChildItemChbox.setChecked(true);
                        data.get(adapterPosition).setisChecked_noChildItem(true);
                    }

                    if (noChildItemChbox.isChecked()) {

                        String drugName = (String) drugsTitle.getText();
                        selectedDrugsList.add(drugName);

                    } else {
                        for (int i = 0; i < selectedDrugsList.size(); i++) {
                            String drugName = (String) drugsTitle.getText();

                            if (selectedDrugsList.get(i) == drugName) {
                                selectedDrugsList.remove(i);
                                Toast.makeText(G.context, drugName + "حذف شد", Toast.LENGTH_SHORT).show();
                            }

                        }
                    }

                }
            });


        }

        void bind(int position) {
            chBoxDoz1.setText(String.valueOf(data.get(position).getdrugDoz1()));
            if (data.get(position).getisChecked_drugDoz1()) {
                chBoxDoz1.setChecked(true);
            } else {
                chBoxDoz1.setChecked(false);
            }

            chBoxDoz2.setText(String.valueOf(data.get(position).getdrugDoz2()));
            if (data.get(position).getisChecked_drugDoz2()) {
                chBoxDoz2.setChecked(true);
            } else {
                chBoxDoz2.setChecked(false);
            }

            chBoxDoz3.setText(String.valueOf(data.get(position).getdrugDoz3()));
            if (data.get(position).getisChecked_drugDoz3()) {
                chBoxDoz3.setChecked(true);
            } else {
                chBoxDoz3.setChecked(false);
            }


            if (data.get(position).getisChecked_noChildItem()) {
                noChildItemChbox.setChecked(true);
            } else {
                noChildItemChbox.setChecked(false);
            }
        }

    }
}

Comments