4

I am trying to change Cursor in CursorAdapter this way:

Cursor newCursor = compiledStatement.getCursor();
startManagingCursor(newCursor);
adapter.changeCursor(newCursor);

Unfortunatelly I get this exception:

java.lang.IllegalStateException: attempt to re-open an already-closed object:
     android.database.sqlite.SQLiteQuery

According to other topics, it should be possible to change content of CursorAdapter without creating new one.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
sealskej
  • 7,281
  • 12
  • 53
  • 64

2 Answers2

5

I have found the problem. My CursorAdapter implements SectionIndexer, so I had to owerwrite changeCursor() method and reset the Cursor for AlphabetIndexer.

@Override
public void changeCursor(Cursor cursor) {
    mIndexer.setCursor(cursor);
    super.changeCursor(cursor);
}
sealskej
  • 7,281
  • 12
  • 53
  • 64
  • This fixed my problem, but now the Garbage Collector goes crazy. I work with a very long Cursor and everything is recreating everytime. Any suggest? – Joaquin Iurchuk Apr 10 '15 at 18:45
2

changeCursor() will close the previous Cursor, which is still managed by the Activity, that is probably the reason you are getting the exception. You might try calling stopManagingCursor() on the old cursor before you call changeCursor().

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • I had same idea, so I have already tried it and with same result: attempt to re-open an already-closed object. – sealskej Aug 30 '11 at 22:30