【问题标题】:Android Cursor - moveToNext() doesnt seem to go to end of cursorAndroid 光标 - moveToNext() 似乎没有走到光标的末尾
【发布时间】:2012-06-07 23:41:56
【问题描述】:

为什么我的 Android 光标没有一直走到原来“承诺”的末尾??

我的 cursor.getCount() 与我上次的 cursor.getPosition() 不同。检查我的while循环!这就是我所做的一切!

注意事项: 1.关于查询联系人内容提供者(android api>5) 2.我只显示基本代码

Cursor cursor = mContext.getContentResolver().query(mUri, mProjections, null, null, null);
Logger.d(TAG, "***  cursor.getCount(): "+cursor.getCount());
while (cursor.moveToNext()) {
    Logger.d(TAG, "| position: "+cursor.getPosition());
    processMainCursor(serializer, cursor);
}
cursor.close();

processMainCursor() 将显示来自光标的数据 + 执行另一个查询:一个 4 个电话、一个 4 个电子邮件、一个 4 个 IM 帐户:

void processMainCursor(XmlSerializer serializer, Cursor main_cursor) {
     writeCursorData(serializer, main_cursor); //writes cursor data, column by column
     writePhoneEntities(serializer, main_cursor);
     writeEmailEntities(serializer, main_cursor);
     writeIMEntities(serializer, main_cursor);
}

在我的writeXXX 方法中,我都没有关闭我的main_cursor 或移动下一步!!!..必须相信我.. 我只是做一个新查询,打印数据并关闭该光标

所以统计:

  • cursor.getCount() = 695 (总是)
  • 评论 writePhoneEntities、writeEmailEntities、writeIMEntities:cursor.getCount() = last cursor.getPosition() = 695(太正确了!
  • 留下一个/两个/所有我的 writeXEntities 显示随机性;示例:将它们全部保留: last cursor.getPosition() 有时会显示 254、257、253 等;只留下电话和 IM:514、510、511 等(如此不同的 RUN -> 不同的 last cursor.getPosition() VALUE

所以意见.. 为什么会这样?和内存有关吗?

更新: 将我的任何 writeXEntities 显示在 logcat 的末尾: 删除死的内容提供者:联系人

更新 2 添加cursor.moveToFirst(); & 像循环一样

do {
    //do whatever you want
} while (cursor.moveToNext()); 

没有完成任务..

所以也许答案就在这个 logcat 条目中:

05-21 23:29:30.209: I/ActivityThread(7085): Removing dead content provider: contacts
05-21 23:29:30.209: I/ActivityThread(7085): Removing dead content provider: com.android.contacts

writeXEntity 的示例已删除

解决方案 ..我没有正确关闭 writeXEntity 的游标(可能在 main while 之后留下相当多的打开游标)

实际上我是这样关闭的

if(phone_cursor!=null && phone_cursor.getCount() > 0)
{                   
     //... stuff
     phone_cursor.close();
}

我应该在 if 之后关闭

if(phone_cursor!=null && phone_cursor.getCount() > 0)
{                   
     //... stuff
}
phone_cursor.close();

我猜要让一个 basilion 光标打开 ..是答案吗?!?

【问题讨论】:

  • 请发布您的一个(或全部)write 功能,您发布的内容应该可以正常工作。
  • 您应该将您的解决方案作为答案发布然后接受它,这样每个人都会知道找到了解决方案(无需打开帖子并阅读它)。

标签: android android-contacts android-cursor


【解决方案1】:

您需要将光标移动到第一行。尝试在while 循环之前添加cur.moveToFirst()

您也可以考虑使用do-while 循环。这将确保您永远不会跳过游标中的第一行:

if (cursor.moveToFirst()) {
    do {
        //do whatever you want
    } while (cursor.moveToNext());               
}
cursor.close();

【讨论】:

  • 按照作者的例子:调用游标,打印语句,使用cursor.moveToNext()。一个新的光标设置为 index: -1 并且moveToNext() 会将索引移动到 0,因此(if 这就是 pulancheck 所做的全部)moveToFirst() 不是必需的,因为moveToNext() 已经移动了第一行的索引。
  • 是的山姆,但为什么代码打印不同的 cursor.getPosition() ..at 不同的运行? (当我的所有/部分 writeXEntity 未注释时会发生这种情况)以及我的最后一个 logCat 消息“删除死的内容提供程序”呢
  • @pulancheck1988 我在这里的评论是为了回应亚历克斯的回答,鉴于上下文,我非常不同意。我不知道为什么您的索引会发生变化,请参阅我对您要求更多代码的问题的评论。
【解决方案2】:

好吧,它们不会是相同的数字,因为 getCount 是项目数,而 position 是位置,(第一个是 0)。所以最终的位置应该总是比计数小一。

如果不是这样,我想我没有正确理解你的问题。

【讨论】:

    【解决方案3】:

    使用光标如下图:

                 if(cursor.getCount() == 0)
                  {                   
                     //No entry found
                  }
                  else  {
                      cursor.moveToFirst();
                      do {
                         //do whatever you want
                         } while (cursor.moveToNext());               
                cursor.close();
    

    【讨论】:

    • 我像你说的那样修改了我所有的光标循环内容&结果:第一次运行-最后一个光标位置:257,第二次运行:256 ..所以仍然没有达到我承诺的 695。
    【解决方案4】:

    阅读您已经找到的答案(关闭游标的问题)后,我认为确保关闭它们的最佳方法是使用以下代码:

    Cursor c = null;
    try {
       c = <your query>;
       if (c.moveToFirst()) { // No point in doing more if empty.
          do {
             <process this cursor row>
          } while (c.moveToNext());
       }
    }
    finally {
       if (c != null) c.close(); // Not sure the check needed, maybe if query was really wrong.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 2012-11-10
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多