我正在使用 loaderManager 从数据库中加载一些结果。不幸的是,以下代码在旋转设备后会产生StaleDataException :
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
{
// If we've returned results, then pass them and the webSearches cursor along to be displayed
if(cursor.moveToFirst())
{
// Get a cursor containing additional web searches and merge it at the end of the results cursor
MatrixCursor searchesCursor = getWebSearchesCursor(searchTerm, false);
Cursor[] cursors = { cursor, searchesCursor };
// TODO: Figure out why merging cursors here causes staledataexception after rotation
Cursor results = new MergeCursor(cursors);
// Display the cursor in the ListView
adapter.changeCursor(results);
}
// If no results were returned, then return suggestions for web searches
else
{
// Get a cursor containing additional web searches
MatrixCursor noResults = getWebSearchesCursor(searchTerm, true);
adapter.changeCursor(noResults);
}
// Show the listView and hide the progress spinner
toggleListView(SHOW);
}
对getWebSearchesCursor()的调用会返回一个 MatrixCursor,其中包含一些附加的搜索提示,以伴随任何返回的结果。我发现将adapter.changeCursor(results)更改为adapter.changeCursor(cursor)可以修复错误,因此看起来将 MatrixCursor 合并到返回的光标会产生错误。
我的问题是,为什么?
如果返回任何结果,我希望能够向返回的光标添加其他项目,以便用户可以选择在几个网站上执行搜索。有没有更好的方法来合并游标,这样我在旋转后就不会出现这个异常?