Android游标获取理解
我想了解我看到的一段代码中的一行:
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;
}
** 之间的以下代码行
是否有必要?今天我花了 2 个小时调试,找出为什么当我调用类似
while(mCursor.moveTonext())
使用光标抓取一些数据的东西时,我的数据丢失了,但最终总是丢失第一个数据。因此,我查看了代码的其他部分,意识到我将整个光标转储到适配器中,因此上面的粗体行没有任何效果。删除这些代码行后,一切都很好!
所以简而言之,如果我只想要一个有1个或多个结果的游标,是否有必要调用上面的粗体语句?谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,当您调用
query()
时,您会获得一组行。最初,光标不会指向任何东西。如果您调用mCursor.moveToNext()
或mCursor.moveToFirst()
,那么您将希望指向第一行。对mCursor.moveToNext()
的其他调用会将光标移动到下一个元组。简而言之,仅当您需要从下一行获取信息(如果存在)时才调用 mCursor.moveToNext() 。
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).