Android的保存和恢复CursorAdapter的国家名单 [英] Android Save and Restore List State with CursorAdapter

查看:191
本文介绍了Android的保存和恢复CursorAdapter的国家名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由CursorAdapter的填充片段的列表视图。

I have a list view in a fragment that is populated by a CursorAdapter.

在设备旋转我要保存列表的状态,则无需进行其它调用的SQLite数据库恢复。

When the device rotates I want to save the state of the list, then restore it without having to make another call to the SQLite DB.

我如何做到这一点?

我不使用ContentProvider的或任何CursorLoaders。

I'm not using a ContentProvider or any CursorLoaders.

推荐答案

您可以挽救你的光标中的方法<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29\"相对=nofollow> onRetainNonConfigurationInstance()您的活动或使用保存光标数据片段,并调用<一个href=\"http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29\"在相对=nofollow> setReatinInstance()片段的的onCreate()方法。

You could save your Cursor in the method onRetainNonConfigurationInstance() of your Activity or use a data fragment that holds the Cursor and call setReatinInstance() within the fragment's onCreate() method.

您然后必须重新创建的CursorAdapter 的onCreate()方法中。

You then have to recreate the CursorAdapter within the onCreate() method.

最后,你必须通过调用<一来存储你的的ListView 的当前可见的最上面的项目href=\"https://developer.android.com/reference/android/widget/AdapterView.html#getFirstVisiblePosition%28%29\"相对=nofollow> getFirstVisiblePosition()在的ListView 。这个方法返回,你可以在你的活动的捆绑的onSaveInstanceState()方法保存一个int值。

Finally you have to store the currently visible topmost item of your ListView by calling getFirstVisiblePosition() on your ListView. This method returns an int value which you can store in a Bundle within the onSaveInstanceState() method of your Activity.

您使用了的onCreate()方法该值(它传给了的onCreate包的一部分())。并且适配器已被设置后,你可以调用[setSelectionFromTop(yourValue,0)](的https://developer.android.com/reference/android/widget/ListView.html#setSelectionFromTop(int, INT))。

You use this value in the onCreate() method (it's part of the bundle passed to onCreate()). And after the adapter has been set, you call [setSelectionFromTop(yourValue, 0)](https://developer.android.com/reference/android/widget/ListView.html#setSelectionFromTop(int, int)).

这篇关于Android的保存和恢复CursorAdapter的国家名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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