0
votes

I want to update a column in my table when CheckBox is checked.

I tried this

Log.d("print", "  " + res.getString(res.getColumnIndex("name")));

But is giving me this error Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

RecyclerViewAdapter:

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d ("print", "item clicked");
                if (checkBox.isChecked()){
                    databaseHandler.updateHabit(name.getText().toString(), false);
                }else{
                    DatabaseHandler databaseHandler = new DatabaseHandler(ctx);
                    databaseHandler.updateHabit(name.getText().toString(), true);
                }
            }
        });

DatabaseHandler:


SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(Constants.Habit.HABIT_COL_DONE, done);

Cursor res = db.rawQuery("SELECT DESCRIPTION from HABIT_DESCRIPTION WHERE NAME = '" + s + "'", null);

res.moveToFirst();
Log.d("print", "  " + res.getString(res.getColumnIndex("name")));

I know it's not updated because when I print my whole table it is the old value.

1

1 Answers

1
votes

I want to update a column in my table when CheckBox is checked

Then you need to use a method that executes a SQL UPDATE query on the table. You already have ContentValues, you can pass those to update().

But is giving me this error Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Your selection has only the DESCRIPTION column but you're trying to read column name. getColumnIndex() returns -1 for a column not present in the cursor.

You should also check the return value of moveToFirst() - it returns true if the move succeeded and the cursor is pointing to a valid row.