如何保存在Android屏幕旋转定制的ArrayList?屏幕、存在、Android、ArrayList

2023-09-12 21:41:02 作者:幻梦吟殇〃

我有,我想能够保存并在屏幕旋转恢复自定义对象的ArrayList。

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

我知道,这是可以做到用的onSaveInstanceState和onRestoreInstanceState如果我做的ArrayList自己的类,它实现了无论是Parcelable或序列化......但是,有没有办法做到这一点,而无需创建另一个类?

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()在的onSaveInstanceState() onRestoreInstanceState()。这种方法将存储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和包)的主题,有时使我的头很疼,这里是一个包含存储在一个捆绑的定制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);
    }
}