光标循环迭代不显示第一个值

发布于 2024-10-24 01:29:01 字数 777 浏览 2 评论 0原文

因此,我有一个返回包含所有值的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过期以后 2024-10-31 01:29:01

我没有看到你的代码有什么问题。那么也许您没有从 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 after cursor.moveToFirst(); to see which is the first element?

暗藏城府 2024-10-31 01:29:01

事实证明代码没有任何问题。我必须在 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.

莫相离 2024-10-31 01:29:01

使用可能会更好

if (cursor.moveToFirst()) {
    while (cursor.moveToNext()) {
        // Your code
    }
}

it might be even better to use

if (cursor.moveToFirst()) {
    while (cursor.moveToNext()) {
        // Your code
    }
}
等待我真够勒 2024-10-31 01:29:01
cursor.moveToFirst();
while (cursor.moveToNext()) {
    // Your code
}

这从来没有让我失望过。尝试一下,看看效果如何?

使用 LOG 进行一些日志记录,看看您得到了什么,并使用以下命令检查数据库的外观
http://sourceforge.net/projects/sqlitebrowser/

编辑:顺便说一句,您不这样做的任何特殊原因是否使用startManagingCursor(cursor);?可能不会导致错误,只是想知道,因为它看起来很方便。

cursor.moveToFirst();
while (cursor.moveToNext()) {
    // Your code
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文