Skip to content Skip to sidebar Skip to footer

Android - Attempt To Re-open An Already-closed Object: Sqlitequery Using Loadermanager

I am fairly new to android and I have some problems with a filtered listView and The activity it's in changing from landscape mode to portrait mode or or vice versa. I have an edit

Solution 1:

Your code is a bit hard to make sense of due to poor formatting.

Anyway, the supplied answer is actually not a fix. The cursor returned at onLoadFinished should be guaranteed not to be closed, so you're loading your cursor in the wrong manner. Specifically, when you call

adapter.getFilter().filter(s.toString());

I don't really understand what goes on here, but I do understand that you should do something else. Just store the query filter in a field within your Fragment and run getLoaderManager().restartLoader(DRINKS_LIST_LOADER, null, this);. Note that you run restartLoader, and not initLoader, because you have different data that you want to query for.

In your onCreateLoader, you should use the filter that you stored as an instance variable for the selection.

Some background

initLoader loads the data that was loaded in the last run, if it had run before. This is why you call in in the initialization method of your Fragment/Activity. This is handy because you won't have to requery on orientation change.

restartLoader cleans up previously loaded data so that you get a new Loader to work with (likely) different data.


If you aren't really sure what you're doing still, make sure to read this article, which is a very good introductory article on Loaders with sample code that looks very much like what you want to achieve. Loaders are pretty enigmatic at first, but once you get the hang of it it's smooth sailing.

Solution 2:

Fixed it:

@Override public void onLoadFinished(Loader cursorLoader, Cursor cursor) { if(!cursor.isClosed()){ adapter.swapCursor(cursor); } }

I do have another problem now, my list does not get updated automatically...

Solution 3:

Adding this line in AndroidManifest helped me in the same situation:

android:configChanges="keyboardHidden|orientation|screenSize"

Solution 4:

This is because after filtering, the original cursor is closed because changeCursor is called. Overriding changeCursor with:

super.swapCursor(cursor)

in your SimpleCursorAdapter makes sure the original cursor is managed by the CursorLoader.

Post a Comment for "Android - Attempt To Re-open An Already-closed Object: Sqlitequery Using Loadermanager"