onLoadFinished()中合并光标旋转后引起StaleDataException [英] Merging cursors during onLoadFinished() causes StaleDataException after rotation

查看:146
本文介绍了onLoadFinished()中合并光标旋转后引起StaleDataException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载使用一个loaderManager数据库的一些结果。不幸的是,下面的code产生的 StaleDataException 旋转设备后:

I'm loading some results from a database using a loaderManager. Unfortunately, the following code produces a StaleDataException after rotating the device:

@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(结果) adapter.changeCursor(光标)修正错误,所以它看起来像一个合并到MatrixCursor返回光标产生错误。

The call to getWebSearchesCursor() returns a MatrixCursor with some additional search prompts to accompany any returned results. I discovered that changing adapter.changeCursor(results) to adapter.changeCursor(cursor) fixes the error, so it looks like merging a MatrixCursor to the returned cursor produces the error.

我的问题是,为什么?

如果任何结果返回,我希望能够更多的项目添加到返回的指针,以便用户有一对夫妇的网站执行他们的搜索选项。有没有更好的方式来合并光标,这样我不旋转后得到这个例外?

If any results are returned, I'd like to be able to add additional items to the returned cursor so the user has the option to perform their search on a couple of websites. Is there a better way to merge cursors so that I don't get this exception after rotation?

推荐答案

这问题又来了前两天过来了,我很幸运在解决绊倒。

This issue came up again a couple of days ago and I was fortunate enough to stumble upon a solution.

我发现我应该使用swapCursor(已经),而不是changeCursor()。 <一href=\"http://developer.android.com/reference/android/widget/CursorAdapter.html#swapCursor%28android.database.Cursor%29\"相对=nofollow>据Android的文档:

I found out that I should have been using swapCursor() instead of changeCursor(). According to the Android docs:

在交换一个新的光标,返回旧的光标。不像changeCursor(光标),返回旧光标没有关闭。

Swap in a new Cursor, returning the old Cursor. Unlike changeCursor(Cursor), the returned old Cursor is not closed.

...

如果给定的新的光标是同一个实例是previously调整光标,空也回来了。

If the given new Cursor is the same instance is the previously set Cursor, null is also returned.

这最后一部分似乎是关键。在上面的问题中提到的错误可以追溯到上CursorAdapter的合并光标窒息,因为当它试图旋转后重绘片段被关闭。通过使用swapCursor()代替,CursorAdapter的是能够重复使用的老合并,而不是质疑其有效性和投掷StaleDataException光标。

That last part seemed to be the key. The error mentioned in the question above could be traced back to the CursorAdapter choking on the merged cursor because it was closed when it tried to redraw the fragment after a rotation. By using swapCursor() instead, the CursorAdapter was able to reuse the "old" merged cursor instead of questioning its validity and throwing a StaleDataException.

我在这里做一些推测;也许有人在Android中的内部运作更有知识可以证实或否认我的推理。

I'm making some suppositions here; perhaps someone more knowledgeable in the inner-workings of Android could confirm or deny my reasoning.

这篇关于onLoadFinished()中合并光标旋转后引起StaleDataException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆