问题描述
boolean android.database.Cursor.moveToNext()文档说:
将光标移动到下一行。
如果游标已经超过结果集中的最后一个条目,则此方法将返回false。 / p>
但是,我的书说要执行以下操作从光标中提取数据:
游标myCursor = myDatabase.query(...);
if(myCursor.moveToFirst()){
do {
int value = myCursor.getInt(VALUE_COL);
//使用值
} while(myCursor.moveToNext());
}
谁是对的?这两个都不是真的。如果看不到矛盾,想象myCursor从查询中返回1行。 getInt()的第一个调用将工作,但是moveToNext()将返回true,因为它不是已经超过结果集中的最后一个条目。所以现在光标将通过最后一个条目,第二次调用getInt()将会做一些未定义的事情。
我怀疑文档是错误的,应该改为:
如果光标为已经在结果集中的最后一个条目。
moveToNext()方法返回false之前,游标是否已经是PAST(不是AT)最后一个条目?
没有Snark请
API的Verbatim: p>
所以,这意味着:
第一行中的游标 - > moveToNext() - >光标在第二行 - >没有第二行 - >返回false
如果你想要的细节,来源:
public final boolean moveToNext(){
return moveToPosition(mPos + 1);
}
public final boolean moveToPosition(int position){
//确保位置不超过游标的结尾
final int count = getCount );
if(position> = count){
mPos = count;
返回false;
}
boolean android.database.Cursor.moveToNext() documentation says:
http://developer.android.com/reference/android/database/Cursor.html#moveToNext%28%29
Move the cursor to the next row.
This method will return false if the cursor is already past the last entry in the result set.
However, my book says to do the following to extract data from a cursor:
Cursor myCursor = myDatabase.query(...); if (myCursor.moveToFirst()) { do { int value = myCursor.getInt(VALUE_COL); // use value } while (myCursor.moveToNext()); }
Who's right? These both can't be true. If you can't see the contradiction, imagine myCursor has 1 row returned from the query. The first call to getInt() will work, but then moveToNext() will return true because it is not "already" past the last entry in the result set. So now the cursor will be past the last entry and the second call to getInt() will do something undefined.
I suspect the documentation is wrong and should instead read:
This method will return false if the cursor is "already at" the last entry in the result set.
Must the cursor be already PAST (not AT) the last entry before the moveToNext() method returns false?
No Snark Please
Verbatim from the API:
So, it means that:
Cursor in first row -> moveToNext() -> cursor in second row -> there's no second row -> return false
If you want the details, go to the source: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.3_r1/android/database/AbstractCursor.java#AbstractCursor.moveToNext%28%29
public final boolean moveToNext() {
return moveToPosition(mPos + 1);
}
public final boolean moveToPosition(int position) {
// Make sure position isn't past the end of the cursor
final int count = getCount();
if (position >= count) {
mPos = count;
return false;
}
这篇关于Android Cursor.moveToNext()文档是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!