0

Log states:

W/Filter(1629): An exception occured during performFiltering()!
W/Filter(1629): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.bazadanych/databases/MyDb
W/Filter(1629):     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
W/Filter(1629):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1156)
W/Filter(1629):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
W/Filter(1629):     at com.example.bazadanych.DBAdapter.fetchCountriesByName(DBAdapter.java:186)
W/Filter(1629):     at com.example.bazadanych.MainActivity$2.runQuery(MainActivity.java:74)
W/Filter(1629):     at android.support.v4.widget.CursorAdapter.runQueryOnBackgroundThread(CursorAdapter.java:397)
W/Filter(1629):     at android.support.v4.widget.CursorFilter.performFiltering(CursorFilter.java:50)
W/Filter(1629):     at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
W/Filter(1629):     at android.os.Handler.dispatchMessage(Handler.java:102)
W/Filter(1629):     at android.os.Looper.loop(Looper.java:136)
W/Filter(1629):     at android.os.HandlerThread.run(HandlerThread.java:61)

As it is in the topic the filter doesn't filter when I go back to listview saying that the cursor has been closed.

The only issue left after using loader, the filter doesn't filter after moving to details activity and coming back to listview. Any ideas?

This is the the filter:

public static Cursor fetchCountriesByName(String inputText) throws SQLException {
    Log.w(TAG, inputText);
    Cursor c = null;

    if (inputText == null  ||  inputText.length () == 0)  {
        c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
            KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_PHONE},
            null, null, null, null, null);
    } else  {
        c = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_PHONE},
                    KEY_NAME + " like '%" + inputText + "%'", null,
                    null, null, null, null);
    }

    if (c != null) {
        c.moveToFirst();
    }
    return c;
}
guipivoto
  • 18,327
  • 9
  • 60
  • 75
Piotr
  • 35
  • 8
  • @Selvin now it's easier to read. – Piotr Mar 30 '15 at 11:28
  • possible duplicate of [IllegalStateException: attempt to re-open an already-closed object. SimpleCursorAdapter problems](http://stackoverflow.com/questions/12358864/illegalstateexception-attempt-to-re-open-an-already-closed-object-simplecursor) – Piyush Mar 30 '15 at 12:24
  • @PiyushGupta thanks but it's not it I've looked at it before – Piotr Mar 30 '15 at 12:41

2 Answers2

1

Varun was close and gave me a lead that I've followed.

Using CursorLoader helped with problems populating the database in a listview, now it loads with no problem, thanks to @Selvin.

The problem was obviously closing cursor, but as I mentioned it was happening only after moving to another activity where I was also using database and there the db has been closed, what affected the possibility of getting data again in the listview after backing to the listview, which used a cursor, but after closing it, retieving it was impossible.

To the problem was: db.close(); not in a listview acitvity but in the other activity. Thanks for all your help.

Piotr
  • 35
  • 8
0

Remove this from your MainActivity.class

@Override
    protected void onDestroy() {
        super.onDestroy();
        closeDB();
    }
  • Didn't help, the method is to prevent data leak from DB. – Piotr Mar 30 '15 at 12:22
  • Seems to be problem in MainActivity line 74 because of DBAdapter class line 186.Please check these lines in both classes. –  Mar 30 '15 at 12:25