给出arrayoutofbound异常.......与android中的游标相关

时间:2015-07-03 06:36:25

标签: android cursor

l = new ArrayList<String>();
        db =openOrCreateDatabase("mydb", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        String sql1 = "create table if not exists words(word text primary key, meaning text not null )";
        db.execSQL(sql1);
        String sql = "select word from words";
        Cursor c = db.rawQuery(sql, null);
        int i=0;
        if (c.isBeforeFirst())
    while(c.moveToNext())
    {
        l.add(c.getString(i));
        i++;
    }

1 个答案:

答案 0 :(得分:0)

只需将条件更改为if(c.moveToFirst())并将while条件设置为结束,否则您可以在从光标获取内容之前移动到下一个语句。您的代码应如下所示:

l = new ArrayList<String>();
db =openOrCreateDatabase("mydb", SQLiteDatabase.CREATE_IF_NECESSARY, null);
String sql1 = "create table if not exists words(word text primary key, meaning text not null )";
db.execSQL(sql1);
String sql = "select word from words";
Cursor c = db.rawQuery(sql, null);
int i=0;
if(c.moveToFirst())
    do
    {
        l.add(c.getString(i));
        i++;
    } while(c.moveToNext())
}