Search code examples
androidsqlitecheckboxandroid-sqlitebaseadapter

Checkbox in a custom adapter to update the database


I am trying to get the checkbox I created in a custom adapter for a list to update the database upon clicking on it. I implemented the onclicklistener on the checkbox inside the customAdapter (code below). I also added a Toast to check if it works. The problem is that the Toast displays the right information for each row (the name of the item i click on); however the database doesn't get updated.

 datasource = new MySQLiteHelper((AssTasksActivity) context);

    CB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Task Ta = task; //Task is an object with a separate class that is stored in the sqlite db.

            if(CB.isChecked()){
                Ta.setState(0);
                datasource.updateTask(Ta);
                //Toast T;
                Toast.makeText(context, Ta.getTaskName(),
                        Toast.LENGTH_LONG).show();
            }
            else{

                Ta.setState(1);
                datasource.updateTask(Ta);
                Toast.makeText(context, Ta.getTaskName(),
                        Toast.LENGTH_LONG).show();
            }
        }
    });

Here is the sqlite code for the update

public int updateTask(Task task){
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(MySQLiteHelper.T_COLUMN_TASK_ID, task.getId());
        values.put(MySQLiteHelper.T_COLUMN_TASKNAME, task.getTaskName());
        values.put(MySQLiteHelper.T_COLUMN_DESCRIPTION, task.getDescription());
        values.put(MySQLiteHelper.T_COLUMN_STATE, task.getState());
        values.put(MySQLiteHelper.T_COLUMN_PK, task.getAssignmentId());
        values.put(MySQLiteHelper.T_COLUMN_ERROR_DESCRIPTION, task.getErrorDescription());

        return database.update(TABLE_TASKS, values, T_COLUMN_ROWID + " = ?",
                new String[] { String.valueOf(task.getId()) });
    }

Solution

  • T_COLUMN_ROWID in the return statement should be T_COLUMN_TASK_ID. Silly mistake.