dimanche 2 janvier 2022

retrieve the state of checkbox as int from SQLite in android studio and show the checkbox state in the activity

i am trying to do health activity in which there is 30 day challenge with 30 checkbox to check one every day ..i need to store the state of checkbox in SQLite and then show the state in the activity i am new in programming and i do not know what is the wrong and why the state of box not saved or may i cant get the value correctly from DB this is my code in DB

public class HealthBoxDataBaseHandler extends SQLiteOpenHelper {
private static final int VERSION=1;
private static final String NAME="healthBox";
private static final String HEALTH_TABLE="healthBoxes";
private static  String ID="id";
private static final String STATUS="status";
private static final String CREATE_HEALTH_TABLE="CREATE TABLE " +HEALTH_TABLE+ "(" +ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
        +STATUS+ " INTEGER)";

private SQLiteDatabase db;

public HealthBoxDataBaseHandler(@Nullable Context context) {
    super(context,NAME,null,VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_HEALTH_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS " +HEALTH_TABLE);
    onCreate(db);

}

public void openDataBase(){
    db=this.getWritableDatabase();
}
public void readData(){db=this.getReadableDatabase();}

@SuppressLint("Range")
public int getStatus(){
    int taskList = -1;
        Cursor cur=null;
        db.beginTransaction();
        try {
            cur=db.query(HEALTH_TABLE,null,null,null,null,null,null,null);
            if (cur!=null){
                if (cur.moveToFirst()){
                    do {
                       
                        /*task.setId(cur.getInt(cur.getColumnIndex(ID)));
                        task.setStatus(cur.getInt(cur.getColumnIndex(STATUS)));*/
                        taskList=cur.getInt(cur.getColumnIndex(STATUS));


                    }while (cur.moveToNext());
                }
            }
        } finally {db.endTransaction();
            cur.close();

        }return taskList;

    }

public void updateStatus(int id,int status){
    ContentValues cv=new ContentValues();
    cv.put(STATUS,status);
    db.update(HEALTH_TABLE,cv,ID+"=?",new String[]{String.valueOf(id)});
}

}

and this my code in health activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_health2);
    getSupportActionBar().hide();
    box1=findViewById(R.id.box1);
    db = new HealthBoxDataBaseHandler(this);
    db.readData();


    box1.setChecked(toBoolean(db.getStatus()));
    box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            onCheckd(box1);
        }
    });

}

public Boolean toBoolean(int n){
    return n!= 0;
}
public void onCheckd(View view){
    boolean checked = ((CheckBox) view).isChecked();
    // Check which checkbox was clicked
    db.openDataBase();
    switch(view.getId()) {
        case R.id.box1:
            if (checked) {

                db.updateStatus(box1.getId(),1);
            } else {

                db.updateStatus(box1.getId(),0);
            }
            break;

    }
}//end onCheckd
@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

}




Aucun commentaire:

Enregistrer un commentaire