光标循环迭代不显示第一个值
因此,我有一个返回包含所有值的 Cursor 的查询(我使用cursor.getCount() 确认了这一点,其中返回的 int 相当于数据库中的记录数),但由于某种原因,无论我进行什么迭代循环写我似乎永远无法检索列表的第一个值。
private void addAllUnsentCrapportsToList() {
mDbAdapter.open();
Cursor cursor = mDbAdapter.getAllCrapports();
cursor.moveToFirst();
String text = "";
while(cursor.isAfterLast() == false){
text = text
+ "typ: " + cursor.getString(cursor.getColumnIndex(DbAdapter.CRAPPORT_KEY_GARBAGETYPE))
+ " kommentar: " + cursor.getString(cursor.getColumnIndex(DbAdapter.CRAPPORT_KEY_COMMENT))
+ "\n";
cursor.moveToNext();
}
unsentCrapportList.setText(text);
}
这段代码显示所有值,不包括第一个值。我做错了什么?我尝试了多种方法,包括 do while、while 等,但总是以同样的错误告终。
我缺少什么?
So, I have a query returning a Cursor with all values (I confirmed this with cursor.getCount() where the int returned was equivalent to the number of records in the database), but for some reason, no matter what iteration-loop I write I can never seem to retrieve the first value of the list.
private void addAllUnsentCrapportsToList() {
mDbAdapter.open();
Cursor cursor = mDbAdapter.getAllCrapports();
cursor.moveToFirst();
String text = "";
while(cursor.isAfterLast() == false){
text = text
+ "typ: " + cursor.getString(cursor.getColumnIndex(DbAdapter.CRAPPORT_KEY_GARBAGETYPE))
+ " kommentar: " + cursor.getString(cursor.getColumnIndex(DbAdapter.CRAPPORT_KEY_COMMENT))
+ "\n";
cursor.moveToNext();
}
unsentCrapportList.setText(text);
}
This bit of code displays all the values, excluding the first. What am I doing wrong? I've tried numerous approaches, including do while, while etc, but have always ended up with the same fault.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我没有看到你的代码有什么问题。那么也许您没有从
db
获取第一个值?您是否尝试过在cursor.moveToFirst();
之后打印数据以查看哪个是第一个元素?I dont see anything wrong with your code. So maybe you are not getting the first value from the
db
? Have you tried to print the data just aftercursor.moveToFirst();
to see which is the first element?事实证明代码没有任何问题。我必须在 Eclipse 中进行“项目清理”,然后重新启动模拟器。无论如何,感谢所有答案。
Turns out there was nothing wrong with the code. I had to to a "project clean" in Eclipse and then restart the emulator. Thanks to all answers anyhow.
使用可能会更好
it might be even better to use
这从来没有让我失望过。尝试一下,看看效果如何?
使用 LOG 进行一些日志记录,看看您得到了什么,并使用以下命令检查数据库的外观
http://sourceforge.net/projects/sqlitebrowser/
编辑:顺便说一句,您不这样做的任何特殊原因是否使用startManagingCursor(cursor);?可能不会导致错误,只是想知道,因为它看起来很方便。
This has never failed me. Try it and see how it goes?
Do some logging with LOG to see what you are getting and check out how the database looks with
http://sourceforge.net/projects/sqlitebrowser/
Edit: Btw, any particular reason you don't use
startManagingCursor(cursor);
? Probably not causing the error, just wondering, since it seems handy.