2010-06-24 44 views
1

在執行某些數據庫操作後,我將光標附加到SimpleCursorAdapter以便在ListActivity上顯示List。如果我關閉遊標在finally塊(或下面的代碼後的任意位置),不顯示在列表中設置爲SimpleCursorAdapter時,無法關閉光標

SimpleCursorAdapter scAdapter = new SimpleCursorAdapter(this, 
      R.layout.list_row, cursor, 
      new String[] { Constants.KEY_TITLE }, new int[] { R.id.text1 }); 

如果光標未關閉的Android拋出異常,當活動使請求到另一個活動或鈍化。

PS我試着在onPause()內部關閉遊標,但是android在onPause被調用之前抱怨。

回答

4

你必須在你的finally塊上打開遊標。

您錯過了致電您的活動。

startManagingCursor(cursor); 

這將確保活動爲您管理光標。

+1

對於那些偶然發現,在較新版本的Android上,不推薦使用'startManagingCursor'。 – 2012-07-27 03:14:01

0

這隻能解決部分問題。當我startManagingCursor(遊標);然後按菜單鍵> menuActivity然後>返回鍵我得到:

ERROR/Cursor(4047): Invalid statement in fillWindow() 
3

我通過創建光標在飛行中解決此問題。看來SimpleCursorAdapter會爲你管理光標。

SimpleCursorAdapter scAdapter = new SimpleCursorAdapter(this, 
     R.layout.list_row, db.query(...), 
     new String[] { Constants.KEY_TITLE }, new int[] { R.id.text1 }); 

此外,由於Pentium10的發佈,startManagingCursor已被棄用。