2010-07-28 85 views
2

光標我提供一個光標到ListActivity這樣的:我的問題是如何/當我應該關閉我的光標,當我做了什麼?我什麼時候應該關閉,我提供給ListView控件

Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
     if (cursor != null) { 
      mCursorAdapter = new CursorAdapter(this, cursor); 
      setListAdapter(mCursorAdapter); 
     } 

謝謝。

回答

4

onDestroy()。更遲的是,如果你明確地清理清單比這更早。或者,撥打startManagingCursor(cursor),Android會在您安全的情況下爲您關閉它。

+0

我應該在我的onDestory做()關閉,我對過時CursorAdpater()光標?或者我必須保持對getContentResolover()。query()的引用,並手動在onDestory()中調用'close()'? 謝謝。 – michael 2010-07-28 19:20:38

+1

@邁克爾:如果你正在使用'getContentResolver()查詢()','那是Cursor'已經進行管理,這樣你就不需要自己關閉它,因爲Android將關閉它。我道歉 - 我專注於遊標,並沒有意識到你已經從ContentResolver而不是SQLite獲得它。 – CommonsWare 2010-07-29 00:45:54

+0

@CommonsWare:如果我把'cursor.close()'行'中onDestroy',光標將被關閉,每次設備方向的變化,這就是不可取的。在這種情況下我能做些什麼? – 2014-07-27 15:52:27

相關問題