Changing of Cursor in CursorAdapter

2019-06-28 00:52发布

问题:

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.

回答1:

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);
}


回答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().