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 をマージするとエラーが発生するようです。
私の質問は、なぜですか?
結果が返された場合、ユーザーがいくつかの Web サイトで検索を実行できるように、返されたカーソルに項目を追加できるようにしたいと考えています。回転後にこの例外が発生しないように、カーソルをマージするより良い方法はありますか?