2011-07-06 47 views
0

我有這種方法,我用它來填充一個列表視圖與sqlite數據庫中的數據。它的工作原理,當應用程序啓動預期:調用startManagingCursor兩次使我的數據消失

private void populateListView() { 
    Cursor showsCursor = mDbHelper.fetchAllSummaries(); 
    startManagingCursor(showsCursor); 

    // Create an array to specify the fields we want to display in the list 
    String[] from = new String[]{DbAdapter.SUMMARY_TITLE, DbAdapter.SUMMARY_DATE, DbAdapter.SUMMARY_SUMMARY}; 

    // and an array of the fields we want to bind those fields to 
    int[] to = new int[]{R.id.showtitle, R.id.showdate, R.id.showsummary}; 

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter shows = 
      new SimpleCursorAdapter(this, R.layout.custom_row_view, showsCursor, from, to); 
    setListAdapter(shows); 
} 

然後我也有再次更新數據庫,並調用populateListView()與更新的數據來填充列表視圖刷新菜單項。這也出現工作正常。

但是沒有!因爲如果我開始另一個活動(例如通過單擊其中一個列表視圖項目),然後使用後退按鈕返回,我的列表視圖是空的。再次調用populateListView()會重新創建它,但當從另一個活動返回時它將再次爲空。

通過調試我發現,如果我省略startManagingCursor()它完美的作品。

所以顯然調用startManagingCursor()兩次會導致意想不到的副作用。爲什麼會發生這種情況,我該如何解決?

是不是調用startManagingCursor()的地方是否應該在onCreate()方法中?我應該實際上在某個時刻調用stopManagingCursor()嗎?

回答

1

調用startManagingCursor()是否應該調用onCreate()方法?

這應該沒問題。

我應該實際上在一個點上調用stopManagingCursor()嗎?

當您不再需要管理Cursor(例如,您正在替換它)時,請致電stopManagingCursor()