2012-01-03 38 views
3

嘿我在我的代码中获取索引超出界限的错误。游标索引超出界限错误Android?

从活动:

  Cursor cursor = mDbHelper.fetchA(b); 
      @SuppressWarnings("static-access") 
      int a = (int) cursor.getDouble(cursor.getColumnIndex(mDbHelper.KEY_I)); 
      cursor.close(); 

我的DB适配器:

public Cursor fetchA(String b){ 
    return mDb.query(DATABASE_TABLE, new String[] {KEY_I}, "b=\""+B+"\"", null, null, null, null); 
} 

错误:

E/AndroidRuntime(5766): FATAL EXCEPTION: main 

    E/AndroidRuntime(5766): android.database.CursorIndexOutOfBoundsException: Index 

    -1 requested, with a size of 1 

我该如何解决这个问题?

回答

5

游标开始指向之前的位置第一行。因此,您需要将光标移至第一行,然后才能获取数据。 Cursor#moveToFirst()#moveToNext()应该完成这项工作。

Cursor cursor = mDbHelper.fetchA(b); 
cursor.moveToNext(); 
@SuppressWarnings("static-access") 
int a = (int) cursor.getDouble(cursor.getColumnIndex(mDbHelper.KEY_I)); 
cursor.close(); 
0
Cursor cursor = mDbHelper.fetchA(b); 
startManagingCursor(cursor); 
cursor.moveToNext(); 
....... 

这里您检索代码。