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?