Android cursor headaches
Could someone please help me to understand why the following block of code keeps throwing this error? It's driving me crazy trying to debug this.
public int getCon开发者_运维知识库tactsCountByGroupId(int id) {
Cursor c = db.rawQuery("SELECT COUNT(*) AS total FROM msg_group_lu WHERE group_id = ?", new String[] {String.valueOf(id)});
DatabaseUtils.dumpCursor(c);
int retval = c.getInt(c.getColumnIndex("total"));
return retval;
}
The dump seems to indicate this; 0 { total=0 }
This is telling me that there is data in column 0, but yet everytime it tries to execute the line with this code;
int retval = new Integer(c.getInt(c.getColumnIndex("total")));
It gives me this error; CursorIndexOutOfBoundsException: Index -1 requested, with size of 1
I have tried everything I can think of to try to fix this and am completely stumped. :(
I hope someone knows what causes this.
try writing c.moveToNext();
before executing the line
int retval = c.getInt(c.getColumnIndex("total"));
The cursor position is now -1. after executing the moveToNext command it comes to 0th position.
精彩评论