I have a listview that is sourced by an sqlite db. I call fillData() at several different points to update the listview.
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;
}
});
}
That works... problem is, when I open/close/jump around activities, I get errors in logcat. It doesn't crash the program though. The errors are:
Please ensure you explicitly call close() on your cursor
and
close() was never explicitly called on database
So under }); if I put mDbHelper.close() it crashes saying database not open. If I put c.close(), my listview never populates. I've tried putting them in various other places and it gives me errors saying cursor/database already closed. Any ideas on what to do?
If everything is in one Activity just open database and cursor in onCreate() and close in onDestroy(). Another option is to use singleton or smoething which will open database or cursor before operations if it's null.
The erros are caused because if you are finishing operations on database you should close it.