如何在Android屏幕旋转上保存自定义ArrayList? [英] How to save custom ArrayList on Android screen rotate?

查看:23
本文介绍了如何在Android屏幕旋转上保存自定义ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义对象的 ArrayList,我希望能够在屏幕旋转时保存和恢复这些对象.

I have an ArrayList with custom objects that I would like to be able to save and restore on a screen rotate.

我知道这可以通过 onSaveInstanceStateonRestoreInstanceState 来完成,如果我要让 ArrayList 成为它自己的类,它实现了 ParcelableSerializable... 但是有没有办法在不创建另一个类的情况下做到这一点?

I know that this can be done with onSaveInstanceState and onRestoreInstanceState if I were to make the ArrayList its own class, which implements either Parcelable or Serializable... But is there a way to do this without creating another class?

推荐答案

您无需创建新类来传递自定义对象的 ArrayList.您应该简单地为您的对象实现 Parcelable 类并使用 Bundle#putParcelableArrayList().此方法将自行存储 Parcelables 的 ArrayList.

You do not need to create a new class to pass an ArrayList of your custom objects. You should simply implement the Parcelable class for your object and use Bundle#putParcelableArrayList() in onSaveInstanceState() and onRestoreInstanceState(). This method will store an ArrayList of Parcelables by itself.

因为 Parcelables(以及 Serializables 和 Bundles)的主题有时让我很头疼,这里是一个包含存储在 Bundle 中的自定义 Parcelable 对象的 ArrayList 的基本示例.(这是可以剪切和粘贴运行的,无需布局.)

Because the subject of Parcelables (and Serializables and Bundles) sometimes makes my head hurt, here is a basic example of an ArrayList containing custom Parcelable objects stored in a Bundle. (This is cut & paste runnable, no layout necessary.)

实现 Parcelable

public class MyObject implements Parcelable {
    String color;
    String number;

    public MyObject(String number, String color) {
        this.color = color;
        this.number = number;
    }

    private MyObject(Parcel in) {
        color = in.readString();
        number = in.readString();
    }

    public int describeContents() {
        return 0;
    }

    @Override
    public String toString() {
        return number + ": " + color;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeString(color);
        out.writeString(number);
    }

    public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() {
        public MyObject createFromParcel(Parcel in) {
            return new MyObject(in);
        }

        public MyObject[] newArray(int size) {
            return new MyObject[size];
        }
    };
}

保存/恢复状态

public class Example extends ListActivity {
    ArrayList<MyObject> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(savedInstanceState == null || !savedInstanceState.containsKey("key")) {
            String[] colors = {"black", "red", "orange", "cyan", "green", "yellow", "blue", "purple", "magenta", "white"};
            String[] numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

            list = new ArrayList<MyObject>();
            for(int i = 0; i < numbers.length; i++) 
                list.add(new MyObject(numbers[i], colors[i]));
        }
        else {
            list = savedInstanceState.getParcelableArrayList("key");
        }

        setListAdapter(new ArrayAdapter<MyObject>(this, android.R.layout.simple_list_item_1, list));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putParcelableArrayList("key", list);
        super.onSaveInstanceState(outState);
    }
}

这篇关于如何在Android屏幕旋转上保存自定义ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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