用户旋转手机时保留列表视图项并将所有数据保留在 ArrayAdapter 中的良好解决方案 [英] Good solution to retain listview items when user rotate phone and keep all data in ArrayAdapter

查看:20
本文介绍了用户旋转手机时保留列表视图项并将所有数据保留在 ArrayAdapter 中的良好解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Fragment 与 listView 一起使用.我通过自定义加载程序(来自互联网)中收到的数据填充与此列表视图关联的 ArrayAdapter.自定义 ArrayAdapter 支持无限滚动(分页).

I'm using Fragment with listView. I fill ArrayAdapter associated with this listview, by data received in custom Loader(from internet). Custom ArrayAdapter supports infinite scrolling(paging).

当用户旋转设备并在 ListView 中保持滚动位置时,在 ArrayAdapter 中存储项目的最佳方式是什么?

What is the best way to store items in ArrayAdapter when user rotate device and keep scroll position in ListView?

我正在考虑使用 ArrayAdapter 创建非可视化 Fragment,并使用 setRetainInstance 方法来保存值.

I'm thinking about creation of non-visual Fragment with ArrayAdapter, and using setRetainInstance method to save values.

对更好的解决方案有什么建议吗?

Any suggestions for better solution?

推荐答案

要使用 Android 框架和 Fragment 生命周期,您应该在 Fragment 中实现 onSaveInstanceState 方法.为简单起见,我假设您有一个可以访问的字符串值数组(我通常扩展 ArrayAdapter 以封装视图构造并提供一种访问整个底层数据集的便捷方法):

To work with the Android framework and Fragment lifecycle you should implement the onSaveInstanceState method in your Fragment. For simplicity I've assumed that you have an array of String values that you can get to (I generally extend ArrayAdapter to encapsulate view construction and to provide a convenience method to access the entire underlying dataset):

public void onSaveInstanceState(Bundle savedState) {

    super.onSaveInstanceState(savedState);

    // Note: getValues() is a method in your ArrayAdapter subclass
    String[] values = mAdapter.getValues(); 
    savedState.putStringArray("myKey", values);

}

然后您可以在 onCreate 方法(或 onCreateView 或 onActivityCreated - 请参阅Fragment JavaDoc) 像这样:

You can then retrieve the data in your onCreate method (or onCreateView or onActivityCreated - see the Fragment JavaDoc) like this:

public void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        String[] values = savedInstanceState.getStringArray("myKey");
        if (values != null) {
           mAdapter = new MyAdapter(values);
        }
    }

    ...

}

这可确保所有生命周期事件都得到正确处理,而不会丢失数据,包括设备旋转和用户切换到其他应用程序.不使用 onSaveInstanceState 和使用内存的危险是 Android 回收该内存的危险.保存的状态不会受此影响,但使用实例变量或隐藏片段会导致数据丢失.

This ensures that all lifecycle events will be handled properly, without loss of data, including device rotation and the user switching to other applications. The danger of not using onSaveInstanceState and using memory is the danger of Android reclaiming that memory. Saved state would not be affected by this but using instance variables or hidden fragments would result in loss of data.

如果 savedStateInstance 为空,则没有要恢复的状态.

If savedStateInstance is null then there is no state to restore.

if (values != null) 只是为了防止没有保存数组的可能性,但是如果您编写 ArrayAdapter 来处理空数据集,则不需要它.

The if (values != null) is simply to guard against the possibility that no array was saved, but if you code your ArrayAdapter to handle a null data set you won't need this.

最终的解决方案,如果您的行是您自己的类之一的实例而不是单个数据项,则是在该类上实现 Parcelable 接口,然后您可以使用 savedState.putParcelableArray("myKey", myArray).您会惊讶于知道如何实现 Parcelable 是多么有用 - 它允许您在意图内部传递您的类并允许您编写更简洁的代码.

The ultimate solution, if your rows are instances of one of your own classes and not single data items, is to implement the Parcelable interface on that class, then you can use savedState.putParcelableArray("myKey", myArray). You'd be surprised how useful it is to know how to implement Parcelable - it allows you to pass your classes around inside intents and allows you to write much cleaner code.

这篇关于用户旋转手机时保留列表视图项并将所有数据保留在 ArrayAdapter 中的良好解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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