【发布时间】:2011-11-14 21:29:27
【问题描述】:
我有一个与in this discussion 描述的问题类似的问题:当底层数据库发生更改时,我需要刷新 ListView,但查询成本很高,所以我在 AsyncTask 中进行。
当更新的光标准备好时,我会这样做。 (这也是列表最初在启动时填充的方式。)
@Override
protected void onPostExecute(Cursor result) {
if (activity != null) {
if (currentCursor != null) {
// existing cursor is closed by adapter.changeCursor() so
// we don't need to explicitly close it here
stopManagingCursor(currentCursor);
}
currentCursor = result;
startManagingCursor(currentCursor);
if (adapter == null) {
adapter = getAdapter(result);
setListAdapter(adapter);
} else {
adapter.changeCursor(result);
}
activity.onGotList(result, dbAdapter);
}
}
这是我得到的错误。并非每次都发生,这更令人沮丧。
Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT DISTINCT t._id AS _id, t.amount, t.date, t.memo, t.synced, t.flag, (children.pa
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:100)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1412)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1382)
所以,我显然没有正确关闭光标。如果我调用currentCursor.close() 而不是依靠adapter.changeCursor() 关闭传出游标,那么我会收到关于关闭游标两次或关闭null 游标的警告。
这样做的正确方法是什么?
在我链接到的讨论中,Dianne Hackborn 建议改用Loader。这不是我的选择,因为我的代码必须在 Android 2.1 上运行。
【问题讨论】:
-
using a Loader instead. That is not an option for me since my code has to run on Android 2.1.是的,这是一个选项,因为有一个兼容库 v4 -
@Selvin 哦,谢谢,我忘记了。我会调查的。
标签: android cursor background-thread