Skip to content Skip to sidebar Skip to footer

Close Was Never Explicitly Called On Database

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() { readDatabase.open(); Curs

Solution 1:

Colse your cursor every time after using then your problem will bo solved

itemsCursor.close()

As you are not closing this, resources of the cursor is not released for that reason when you close your db you are getting that error.

Make Your cursor as global variable then on your onDestroy

@Override
protected void onDestroy() {
    super.onDestroy();
    itemsCursor.close();
    db.close();
}

And as you are now adding close statement as a last statement of filldata method, the Adapter of listview doesn't get any data as cursor is already released for that reason you are not getting any data in listview.


Solution 2:

You should close your cursor object and the database object in the onDestroy() method,

if (itemsCursor!=null){
    itemsCursor.close();
}
if (readDatabase!=null){
    readDatabase.close();
}

Edit- Have you tried closing the cursor at the end of the fillData() function,

@SuppressWarnings("deprecation")
private void fillData() {
    readDatabase.open();
    itemsCursor = readDatabase.fetchAllItems();


    startManagingCursor(itemsCursor);

    String[] from = new String[] { DatabaseHandler.KEY_ITEM,
            DatabaseHandler.KEY_UNITCOST, DatabaseHandler.KEY_QUANTITY,
            DatabaseHandler.KEY_TOTAL };

    int[] to = new int[] { R.id.itemtext, R.id.costtext, R.id.quantitytext,
            R.id.totaltext };

    SimpleCursorAdapter items = new SimpleCursorAdapter(this,
            R.layout.rowincart, itemsCursor, from, to);

    setListAdapter(items);
    if (itemsCursor!=null){
    itemsCursor.close();
     }
    if (readDatabase!=null){
        readDatabase.close();
      }
    }

Post a Comment for "Close Was Never Explicitly Called On Database"