首页 > 解决方案 > 为什么即使在 moveToNext() 之类的调用之后,android SQLite 光标仍位于位置 -1?

问题描述

这段代码曾经可以工作,但现在当我进入 for 循环中的 Cursor.get 方法时,我得到:

android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为5

Cursor.getCount() 方法大于零,但光标仍不移动。

我正在使用 SQLiteAssetHelper。这是我的代码:

ArrayList<Profile> GetAllProfiles() throws SQLiteException{
    SQLiteDatabase database = this.getReadableDatabase();
    String[] selection = {COL_ID, COL_NAME, COL_IMAGE};
    Cursor c = database.query(TABLE_PLAYERS, selection, null, null, null, null, null);

    ArrayList<Profile> profiles = new ArrayList<>();

    int colIndexID = c.getColumnIndex(COL_ID);
    int colIndexName = c.getColumnIndex(COL_NAME);
    int colIndexImage = c.getColumnIndex(COL_IMAGE);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        profiles.add(new Profile(
        c.getInt(colIndexID),
        c.getString(colIndexName),
        c.getBlob(colIndexImage)));
    }

    database.close();
    return profiles;
}

标签: javaandroiddatabasesqlite

解决方案


推荐阅读