【问题标题】:Android HoneyComb - Requering already closed cursorAndroid HoneyComb - 请求已经关闭的光标
【发布时间】:2012-11-25 03:39:30
【问题描述】:

我阅读了很多关于这个问题的问题,但我找不到解决问题的正确方向,可能是因为我是菜鸟并且迫切希望找到正确的方法......

据我所知,“请求已经关闭的游标”问题发生在 HoneyComb 之后,因为 startManagingCursor 已被弃用。由于 HoneyComb 我们应该使用与ContentProvider 一起使用的CursorLoader(我不使用也不想使用),并且发现有人写了一个类来使用CursorLoader 而没有ContentProvider(@ 987654321@).

在我的应用程序中,我有一个 mySQLiteHelper 类,我有很多方法可以从 SQLite 检索数据,例如:

    public Cursor listaBeneficios(String id) {
    String where = "proposta=?";
    String[] whereArgs = {id};
    String[] campos = {"pessoa"};
    Cursor c = db.query(TABLE_BENEFICIOS, campos, where, whereArgs, null, null, null, null);
    if (c.moveToFirst()) {
        return c;
    } else {
        return null;
    }
}

在我的Activity 上,我可以像这样读取数据:

    db = new repositorio(MainActivity.this);
    Cursor c = db.listaBeneficios(PROPOSTA);
    startManagingCursor(c);
    address.setText(c.getString(c.getColumnIndex("address")));
    db.close();

但我注意到,每次我点击后退按钮并返回活动 A 时,都会出现臭名昭著的“试图重新查询已经关闭的光标”。 我已经尝试过 stopManagingCursor 和 close() 并且不使用 startManagingCursor。 这让我发疯了。

有没有使用CursorLoaders 的简单示例? 如果我实现CursorLoaders,我是否能够继续使用上面的方法(即上面的 listaBeneficios())? 这class 对我有帮助吗?如何实现?

感谢任何帮助。

【问题讨论】:

  • startManagingCursor(c) 被弃用是有原因的 :)
  • 大声笑我知道 :) 但我该如何解决这个问题?我是否能够继续使用现有方法通过 cursorloader 从 sqlite 检索数据?如果是,怎么做?
  • 最简单的方法是使用无管理游标方法。只需自己打开和关闭光标。否则,请使用您链接的加载程序并实现加载程序回调,如mobile.tutsplus.com/tutorials/android/…
  • 看看这个:helpmeco.de/2012/3/…
  • @Tudor 感谢所有这些链接,但我也可以用谷歌搜索,正如我所说,我已经阅读了很多关于这方面的内容,我不想使用内容提供商(如果可能的话)

标签: android sqlite sqliteopenhelper android-cursor android-cursorloader


【解决方案1】:

CursorLoader 不起作用,因为它需要内容 URI。但是,您始终可以使用LoaderAsyncTaskLoader。这些类的documentation 包含如何使用它们的示例。

【讨论】:

    【解决方案2】:

    在我的例子中,我使用 managedQuery 从 ContentProvider 将光标返回到我的活动。我只是将调用更改为 getContentResolver().query 并解决了问题。

    在 Eclipse 中执行 Control H 以换出您需要的所有更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多