Android 更改光标会导致异常

Android 更改光标会导致异常,android,cursor,textwatcher,Android,Cursor,Textwatcher,我在对话框中有一个ListView和一个EditText来过滤我的列表中的CustomerCodes,我用一个TextWatcher实现了我的过滤查询,在onTextChanged()中,我用 Cursor FilteredCPCodeList = CustomerDBAdapter.instance.CursorFilteredCPCode(s.toString()); //Retrieve Filtered CustomerCodeList CpListadapter.changeCurs

我在对话框中有一个ListView和一个EditText来过滤我的列表中的CustomerCodes,我用一个TextWatcher实现了我的过滤查询,在onTextChanged()中,我用

Cursor FilteredCPCodeList = CustomerDBAdapter.instance.CursorFilteredCPCode(s.toString());  //Retrieve Filtered CustomerCodeList
CpListadapter.changeCursor(FilteredCPCodeList);
列表筛选对上面的代码非常有效,但当我单击列表项时,使用旧游标的Listener会导致异常,该异常告诉:

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.
我知道更改游标将关闭我的旧游标,但我不知道如何在旧游标(或其他解决方案)上使用StopManagingCursor来解决此问题。我在onTextChanged()上尝试了此代码,但也不起作用

Cursor OldCursor = CpListadapter.getCursor();
stopManagingCursor(OldCursor );
感谢您的帮助。

stopManagingCursor()
已被弃用,不再推荐使用。你应该使用。然后可以使用
SimpleCursorAdapter
swapCursor(游标)
方法


如果您需要使用当前设置,您应该能够执行
CpListadapter.getCursor().close()
(例如,在您的
onDestroy()
)中)。

谢谢您,oleg用swapcursor替换change cursor解决了我的问题。是的,与
changeCursor()
不同,
swapcursor()
不会关闭原始设置。请注意,它需要API 11,除非您使用兼容性库中的
android.support.v4.widget.SimpleCursorAdapter