1

I want an alertDialog with multi choices items (items from my DB), but when I use it, I have this error: "android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2".

My code to get data from db:

public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_NAME_2,null);
    return res;
}

My DB:

public static final String TABLE_NAME_2 = "word_table";
public static final String COL1_1 = "ID";
public static final String COL1_2 = "NAME";
public static final String COL1_3 = "VALUE";
public static final String COL1_4 = "ISSELECTED";

db.execSQL("create table " + TABLE_NAME_2 +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,VALUE TEXT,ISSELECTED INTEGER)");

My code using multichoiceit

Cursor res = db.getAllData();
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMultiChoiceItems(res,res.getString(3),res.getString(1),new DialogInterface.OnMultiChoiceClickListener() {

        @Override
        public void onClick(DialogInterface parent, int position,
                            boolean checked) {

        }

    });
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.show();

error message (I have only 2 records in my db):

android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2

thank a lot for your help!

1 Answers1

0
if ( res.getCount() < 4 )
    return;

res.getString(3) // can safely be called now

but then i do not understand your code as the definition is

 public AlertDialog.Builder setMultiChoiceItems (Cursor cursor
   , String isCheckedColumn, String labelColumn
   , DialogInterface.OnMultiChoiceClickListener listener) 

I would rather think you would call it like:

 builder.setMultiChoiceItems(res, "0", "1", .....         
 builder.setMultiChoiceItems(res, "ischecked", "label", .....

As you now posted

public static final String COL1_3 = "VALUE";
public static final String COL1_4 = "ISSELECTED";

It should be:

builder.setMultiChoiceItems(res, COL1_4, COL1_3, .....
greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Where do I put that? – Mathias Hanquiniaux Dec 28 '17 at 14:05
  • Before use of the statement or function that causes the index out of bounds exception. – greenapps Dec 28 '17 at 14:12
  • Cursor: the cursor used to provide the items. isCheckedColumn : specifies the column name on the cursor to use to determine whether a checkbox is checked or not. It must return an integer value where 1 means checked and 0 means unchecked. labelColumn : The column name on the cursor containing the string to display in the label. The problem come from my cursor... But I dont know why. – Mathias Hanquiniaux Dec 28 '17 at 14:47