【发布时间】:2017-12-28 23:22:54
【问题描述】:
我想要一个包含多项选择项(我的数据库中的项目)的 alertDialog,但是当我使用它时,出现以下错误:“android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2”。
我从数据库中获取数据的代码:
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME_2,null);
return res;
}
我的数据库:
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)");
我的代码使用 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();
错误信息(我的数据库中只有 2 条记录):
android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
非常感谢您的帮助!
【问题讨论】:
-
请告诉
res.getString(3)和res.getString(1)的值。 -
添加您的 TABLE_NAME_2 结构,您似乎只有两列,但您在第三列建立了索引。
-
完成,查看“我的数据库”。
-
检查光标是否为空并使用 cursor.moveToFirst() 获取起始索引。
-
看看这个Question & 大概看看这个answer also
标签: android