Arash GM
Arash GM

Reputation: 10395

Changing Cursor cause an Exception

I have a ListView in a dialog and an EditText to filter my list for CustomerCodes,i've implemented my filter query with a TextWatcher and in onTextChanged() i've changed my old Cursor with

Cursor FilteredCPCodeList = CustomerDBAdapter.instance.CursorFilteredCPCode(s.toString());  //Retrieve Filtered CustomerCodeList
CpListadapter.changeCursor(FilteredCPCodeList);

List Filtering works Perfect with codes above but when i click on a ListItem it's OnItemClickListener which use old Cursor cause an Exception which tells :

01-05 10:33:01.577: E/AndroidRuntime(5380): android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.

i know that change cursor will close my old cursor but i don't know how can i use StopManagingCursor on my old cursor(or another soloution) to solve this Issue.i've tried this code on onTextChanged() but it doesn't work either

Cursor OldCursor = CpListadapter.getCursor();
stopManagingCursor(OldCursor );

any help would be appreciated,Thanks

Upvotes: 0

Views: 2959

Answers (1)

Oleg Vaskevich
Oleg Vaskevich

Reputation: 12672

stopManagingCursor() is deprecated and not recommended anymore. You should be using CursorLoader. Then you can use a SimpleCursorAdapter along with the swapCursor(Cursor) method.

If you need to use your current setup, you should be able to do CpListadapter.getCursor().close() (for example, in your onDestroy()).

Upvotes: 1

Related Questions