lundi 11 octobre 2021

check the selected checkbox in android

There is a dynamic set of checboxes and I want to save the clicked checkbox state. I have been able to have a correct position of checkbox in the db but I can not use it to check the right checkbox. Every time I select a checkbox, when I reopen that, it is unchecked. Here is the implementation

 for (int check_pos= 0; index < array.get(check_pos).length; check_pos++) {
                                    
     checkBox = new CheckBox(this);
     checkBox.setId(check_pos);
     checkBox.setText("cb");
     layout.addView(checkBox);
     checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
               checkbox_id = buttonView.getId();
               int checkbox_pos = db.readPos(checkbox_id));
               selectItem();

          //     checkBox = checkBox.findViewById(checkbox_pos);
          //     checkBox.setChecked(true);
            }
       });
                                      
 }

When I reopen the checkboxes, the checkbox_pos returns the correct position and works fine as well but the only problem is that selected checkbox does not stay checked. I have tried something above its giving null pointer exception and nothing has worked so far.




Aucun commentaire:

Enregistrer un commentaire