android.database.CursorIndexOutOfBoundsExceptio...

2426 ワード

分類:android
2012-03-26 20:34 
318人が読む
コメント(0)
コレクション
通報する
android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1
Androidでのデータベース処理、特にcursorを使用する場合は、初期位置に注意してください.下に-1と表記されているところから始まるようです.つまり、1回のクエリでは、cursorクエリの結果に戻ると、すぐにcursorから値を抽出できません.
例えば、次のコードはエラーを返します.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 0:

int score = ((Cursor)getReadableDatabase().query(TABLE_NAME, new String[]{"learned"}, "_id=?", new String[]{""+id}, null, null, null,"1")).getInt(0);


正しい使い方:

Cursor cursor = getReadableDatabase().query(TABLE_NAME, new String[]{"learned"}, "_id=?", new String[]{""+id}, null, null, null,"1"); int learned=0; if(cursor.moveToFirst()){ score= cursor.getInt(0); } cursor.close();