android - Cursor.moveToNext() 返回 false 而 Cursor.getCount() 表示超过 1 行

标签 android sqlite

我正在我的 Sqlite 数据库中运行查询..

我将结果保存在我的 Cursor 中,我正在遍历它以获得所有结果..

allotugh 我正在检查并看到游标有超过 1 行,它只给我第一行,因为 moveToNext() 总是返回 false..

这是我的迭代代码:

Cursor cursor = getEventsDetails(db, selection, null);              
Log.e("cursor before", "count"  + cursor.getCount());
boolean moveToNext = cursor.moveToFirst();
if (moveToNext)
{
    do 
    {                               

        //Create a new PicoEvent instance with the event's details
        PicoEvent event = PicoEventCreator.createPicoEvent(cursor);         
        Log.e(event.getName(), event.getStartTime().toLocaleString());
        Log.e("cursor in", "count"  + cursor.getCount());
        moveToNext = cursor.moveToNext();
        Log.e("moveToNext", "move: " + moveToNext);
        if (!isEventExists(eventsList, event))
            eventsList.add(event);
    } while (moveToNext);
}   

这是 Logcat 调试:

12-01 17:33:07.774: E/cursor before(5950): count4
12-01 17:33:08.094: E/Pico Demo Event(5950): 1 בדצמ 2013 11:00:00
12-01 17:33:08.124: E/cursor in(5950): count4
12-01 17:33:08.124: E/moveToNext(5950): move: false

如您所见,行数是 4,但移动是错误的...

最佳答案

您正在将此 Cursor 传递给此调用中的另一个方法:

PicoEvent event = PicoEventCreator.createPicoEvent(cursor);

我敢打赌,该方法会在光标返回之前将光标推进到末尾。

关于android - Cursor.moveToNext() 返回 false 而 Cursor.getCount() 表示超过 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20313896/

相关文章:

Java - 动态添加数据到 String[ ] 和 Composer[ ][ ]

Android SQLite Journal 行为改变了吗?

SQLite:在计算中使用别名

android - 这是否在 UI 线程上运行

android - 如何在 adb shell 中跟踪路由?

android - 如何调整背景图像大小以适应Android中的应用程序屏幕尺寸

sql - 带联接和组的SQLite SELECT?

android - 不需要的 FrameLayout 拉伸(stretch)以包裹所有 child 的高度(而不是只考虑一个 child 的高度)

python - 通过 Python 类实现元组和 row_factory 功能

java - 安卓查询参数