-1

I have the following code:

public Cursor getListText(String [] cols) {
    Cursor cursor = null;

    if(cols != null && cols.length > 0) {
        openDb(false);
        cursor = db.query(true, Contract.Text.TABLE_NAME, cols, null, null, null
                , null, null, null);

        if(!cursor.moveToFirst()) {
            cursor = null;
        }
        closeDb();
    }

    return cursor;
}

Cursor returned by getListText(String [] cols) is consumed by SimpleCursorAdapter to show a ListView.

If I dont invoke to cursor.moveToFirst() Android throws an exception:

java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.

Why this happen? It is truth that this occur only if I close the database closeDb();

If it is necessary I attach the SimpleCursorAdapter implementation:

private void listViewSetup() {
    this.listView = (ListView) findViewById(R.id.texts);
    CursorAdapterDto cursorAdapterDto = this.presenter.getListTextNames();
    Cursor cursor = cursorAdapterDto.getCursor();
    Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(cursor));
    String [] contractNames = cursorAdapterDto.getContractNames();
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.custom_list_item, cursor,
            new String [] {"title"}, new int[] {R.id.itemTextView} , 2);

    listView.setAdapter(adapter);

    if(cursor == null) {
        Toast.makeText(this, "No hay textos",
                Toast.LENGTH_LONG).show();
    }
}

Thank you for your help!

EDITED Sorry! I think that I did not explain the question! I know that I have to manage the cursor before close the database. What I want to know is how SimpleCursorAdapter use the cursor. I mean If I move to first the cursor and close the database SimpleCursorAdapter paint the listView items! But If I do not move to first and close de database why I cannot access to the items? Why the cursor content is not available?

Erik Lucio
  • 948
  • 7
  • 8

3 Answers3

1

When you close the database, it releases the Cursor. Don't close the database until you are done with the Cursor. If you're using the Cursor in an Activity or Fragment, generally, you can close it at the end of the Activity/Fragment lifecycle, usually in onDestroy() or onStop().

In addition to concerns about releasing the Cursor, you should also be using the same database connection for all database actions in that Activity/Fragment, rather than creating/closing a connection for every database action. Consider implementing a database singleton to handle the creation of the connection in this manner.

RedBassett
  • 3,469
  • 3
  • 32
  • 56
0

Erik, close database must be the last operation always. you cant use Cursor after close database, if you do throws an exception.

In other words, you cant go into a database and try to read rows (Which is the function of the cursor) if the DB is closed...

A more detailed explanation you can see in this link. (Android: Cannot perform this operation because the connection pool has been closed)

I hope it helps.

Community
  • 1
  • 1
Diekrul
  • 136
  • 7
0

You may need to call moveCursorToFirst() yourself in your program. This here may help you. I think that by not calling moveCursorToFirst() yourself, it is being checked in your if() and the Database is closing then, which is causing the error.

J2112O
  • 636
  • 3
  • 11
  • 24