2011-04-18 58 views
3

我有一個由sqlite數據庫來源的列表視圖。我在幾個不同的點調用fillData()來更新列表視圖。關閉從未明確呼籲

private void fillData() { 
     mDbHelper.open(); 
     Cursor c = mDbHelper.fetchAllNotes(table, columns, selection, selectionArgs, null, null); 
     startManagingCursor(c); 

     String[] from = new String[] { "quantity", "color", "style" }; 
     int[] to = new int[] { R.id.textQuantity, R.id.textColor, R.id.textStyle }; 
     setListAdapter(new SimpleCursorAdapter(this, R.layout.recordrow, c, from, to) { 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View v = super.getView(position, convertView, parent); 
       return v; 
      } 
     }); 
} 

這有效......問題是,當我打開/關閉/跳轉活動,我得到logcat錯誤。它不會使程序崩潰。的錯誤是:

請確定您明確調用close()上的光標

close()方法從未被明確要求在數據庫

所以下});如果我把mDbHelper.close()它崩潰說數據庫沒有打開。如果我把c.close(),我的列表視圖永遠不會填充。我試圖把它們放在其他地方,它給我錯誤說光標/數據庫已經關閉。有什麼想法做什麼?

+0

僅供參考,startManagingCursor方法已棄用。你可能想刪除它,然後在})之後放置mDbHelper.close()。 – chaitanya 2011-04-18 19:36:06

+0

如果我這樣做,列表視圖會填充,但我會得到幾個'正在完成尚未停用或關閉的遊標'錯誤。 – Roger 2011-04-18 19:41:57

回答

2

如果一切都在一個Activity中,只需在onCreate()中打開數據庫和光標並在onDestroy()中關閉即可。 另一種選擇是使用singleton或smoething,如果它爲空,它將在操作之前打開數據庫或遊標。

這個錯誤是因爲如果你正在完成對數據庫的操作,你應該關閉它。