I am trying to save the state of checkbox in android. The problem is that as soon as I select a checkbox and close the dialog and reopen the dialog, the checkbox does not stay checked.
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();
// checkBox.setChecked(true); => selects the clicked and last item and uncheck
// too
Toast.makeText(this , checkbox_id, Toast.LENGTH_SHORT).show();
}
});
// checkBox.setChecked(true); => checks all items and they stay checked after
// closing dialog
}
I have tried using the checkBox.setChecked(true);
inside the onCheckedChangeListener
but it automatically selects the clicked item and last item and they uncheck too. I don't know how to keep only the selected item.
Aucun commentaire:
Enregistrer un commentaire