android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5

描述:
01-02 00:13:43.380: E/flyLog:ChatManager(963): getUnreadChatGroupandroid.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
01-02 00:13:43.380: E/flyLog:ChatManager(963): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
01-02 00:13:43.380: E/flyLog:ChatManager(963): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)

01-02 00:13:43.380: E/flyLog:ChatManager(963): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)


My code is like this:

<span style="white-space:pre">	</span>cursor = db.rawQuery("select id,group_name,group_type from chat_group g where group_type in( "+p.toString()+" ) and exists(select 1 from user_chat_unreads c where c.group_id=g.id and c.status=0 and c.user_id=? ) order by last_time desc;", params);
			
	if(cursor.moveToFirst()){
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.move(i);
			ChatGroup chatGroup = new ChatGroup();
			String id = cursor.getString(0);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}

cursor.move was copied from the Internet, and it was said that it was obtained this way. I couldn’t see the problem. I debugged carefully and found no code problems, so I looked at the official API.

Move the cursor by a relative amount, forward or backward, from the current position. Positive offsets move forwards, negative offsets move backwards. If the final position is outside of the bounds of the result set then the resultant position will be pinned to -1 or count() depending on whether the value is off the front or end of the set, respectively. 
This method will return true if the requested destination was reachable, otherwise, it returns false. For example, if the cursor is at currently on the second entry in the result set and move(-5) is called, the position will be pinned at -1, and false will be returned. 


It means that the i of Cursor's move(i) is the relative amount of movement, that is, move i forward or backward relative to the current value. If i is positive, it moves backward, and if i is a negative number, it moves forward.

What the hell

Decisively changed it to moveToPosition(int position), and it worked. It seems that we can't trust the Internet, otherwise it would be better to have no Internet.

Or refer to other methods to change the code processing method:
moveToNext() 
 is equivalent to move(1)
moveToPrevious() 
 is equivalent to move(-1)

Guess you like

Origin blog.csdn.net/u013456765/article/details/42340809