vendredi 4 mai 2018

Adding a value when a checkbox is checked

I want to transfer a value to another fragment when the view is clicked, the value I want to transfer is the number of checkboxes checked, for that I have this code.

private int checkCheckBoxes(int total) {
        View habitosView = (LayoutInflater.from(mContext)).inflate(R.layout.habitos_list_row, null);
        CheckBox CB1 = habitosView.findViewById(R.id.CBdia1);
        CheckBox CB2 = habitosView.findViewById(R.id.CBdia2);
        CheckBox CB3 = habitosView.findViewById(R.id.CBdia3);
        CheckBox CB4 = habitosView.findViewById(R.id.CBdia4);
        CheckBox CB5 = habitosView.findViewById(R.id.CBdia5);
        CheckBox[] checkBoxes = new CheckBox[5];
        checkBoxes[0] = CB1;
        checkBoxes[1] = CB2;
        checkBoxes[2] = CB3;
        checkBoxes[3] = CB4;
        checkBoxes[4] = CB5;
        int habitosTotal = total;
        for (int x=0; x<5; x++){
            if(checkBoxes[x].isChecked()){
                habitosTotal = habitosTotal +  1;
            }
        }
        Log.d("Habitos total" , habitosTotal+"");
        return habitosTotal;

};

And for the OnClick of the view I have this public void onBindViewHolder(MyViewHolder holder, final int position){

    holder.nombre.setText(mHabitosList.get(position).getNombre());
    holder.habitosCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int total = mHabitosList.get(position).getTotal();
            checkCheckBoxes(total);
            AppCompatActivity activity = (AppCompatActivity) view.getContext();
            FragmentHabitos fragmentHabitos= new FragmentHabitos();
            fragmentHabitos.setArguments(args);
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentHabitos)
                    .addToBackStack(null).commit();
        }
    });
}

Its returning 0 no matter what, can someone point me out the error. I could not find it.




Aucun commentaire:

Enregistrer un commentaire