I would like to understand a line in a piece of code I saw:
public Cursor fetchMessageByMessageId(String msgId) {
Cursor mCursor =
mDb.query(true, DATABASE_MESSAGES_TABLE, new String[] { KEY_ROWID,
KEY_CONVERSATION_ID, KEY_MSG_ID, KEY_TITLE, KEY_BODY,
KEY_IS_REPLY, KEY_MEDIA_LOC, KEY_URL, KEY_TIMESTAMP },
KEY_MSG_ID + "='" + msgId + "'", null, null, null, null, null);
**if (mCursor != null) {
mCursor.moveToFirst();
}**
return mCursor;
}
The following lines of code in between **
Is this line necessary? I spent 2 hours debugging today finding out why my data was missing when I called something like
while(mCursor.moveTonext())
use the cursor to grab some data and ended up missing the first data always. So I looked at my other parts of the code and realised that I dumped the whole cursor into the adapter so the bold line above had no effect whatsoever. After removing those lines of code everything was good!
So in short, if I just want a cursor with 1 result or many, is it necessary to call the bold statement above? Thanks!
Basically, you get a set of row when you call
query(). Initially the cursor will be pointing to nothing. If you callmCursor.moveToNext()ormCursor.moveToFirst(), then you will want to point the the first row. Additional calls tomCursor.moveToNext()will move the cursor to next tuple.In short, only call
mCursor.moveToNext()when you need to get information from next row (if it exists).