I am trying to check a checkbox and keep its state to checked until it gets unchecked but as soon as I close the dialog containing the checkboxes and come back to it, all the checkboxes are checked instead of the selected one. I have tried the following way
for (int index= 0; index < array.get(index).length; index++) {
checkBox = new CheckBox(this);
checkBox.setId(index);
checkBox.setText(index);
layout.addView(checkBox);
checkBox.setChecked(db.getCheckBoxStatus(
String.valueOf(items.getId()),
checkbox_position));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkbox_position = buttonView.getId();
boolean success = db.update(String.valueOf(items.getId())
, String.valueOf(checkbox_position)
, isChecked);
if (success){
setItem();
}
});
}
To keep the selected one checked I am doing something like below
checkBox.setChecked(db.getCheckBoxStatus(
String.valueOf(items.getId()),
checkbox_position));
}
public static final String _ID = "_id";
public static final String CHECKBOX_STATUS = "the_status";
private static final String CHECKBOX_POSITION = "checkbox_position";
public boolean getCheckBoxStatus(String _id, int checkbox_position) {
boolean rv = false;
SQLiteDatabase database = this.getWritableDatabase();
Cursor csr = database.query(TABLE_NAME,null,_ID +
"=?",new String[]{_id}
,null,null,null);
if (csr.moveToFirst()) {
rv = csr.getInt(csr.getColumnIndex(CHECKBOX_STATUS)) > 0;
}
csr.close();
return rv;
}
public boolean update(String _id,String checkbox_position, boolean isChecked){
SQLiteDatabase database = this.getWritableDatabase();
String selection = "_id = ?";
ContentValues contentValues = new ContentValues();
contentValues.put(_ID,_id);
contentValues.put(CHECKBOX_POSITION,checkbox_position);
contentValues.put(CHECKBOX_STATUS,isChecked);
long result = database.update(TABLE_NAME , contentValues , _ID + " =? " ,
new String[]{_id});
if (result == -1){
return false;
} else {
return true;
}
}
Is there any way I can get it to check only the selected items? Thanks for help.
Aucun commentaire:
Enregistrer un commentaire