Parcelable不会解组嵌套对象

时间:2016-01-22 21:44:09

标签: android android-intent parcelable

我包括下面的整个结构,所以你可以看到我做了什么。但它没有用。当我跨活动发送NutHouse对象时,NutHouse到达但ArrayList为空。

public class NutHouse implements Parcelable{
    Safety safety = Safety.NICE;
    String name;
    String address;
    ArrayList<Dog> dogs;

    protected NutHouse(Parcel in) {
        safety = in.readParcelable(Safety.class.getClassLoader());
        name = in.readString();
        address = in.readString();
        dogs = in.createTypedArrayList(Dog.CREATOR);
        mice = in.createTypedArrayList(Mouse.CREATOR);
    }

    public static final Creator<NutHouse> CREATOR = new Creator<NutHouse>() {
        @Override
        public NutHouse createFromParcel(Parcel in) {
            return new NutHouse(in);
        }

        @Override
        public NutHouse[] newArray(int size) {
            return new NutHouse[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(safety, flags);
        dest.writeString(name);
        dest.writeString(address);
        dest.writeTypedList(dogs);
        dest.writeTypedList(mice);
    }
}

class Dog implements Parcelable{
    String name;
    String owner;
    ArrayList<Show> shows;
    HashMap<String,String> friends;

    protected Dog(Parcel in) {
        name = in.readString();
        owner = in.readString();
        shows = in.createTypedArrayList(Show.CREATOR);
    }

    public static final Creator<Dog> CREATOR = new Creator<Dog>() {
        @Override
        public Dog createFromParcel(Parcel in) {
            return new Dog(in);
        }

        @Override
        public Dog[] newArray(int size) {
            return new Dog[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(owner);
        dest.writeTypedList(shows);
    }
}

class Show implements Parcelable{
    String address;
    int placement;

    protected Show(Parcel in) {
        address = in.readString();
        placement = in.readInt();
    }

    public static final Creator<Show> CREATOR = new Creator<Show>() {
        @Override
        public Show createFromParcel(Parcel in) {
            return new Show(in);
        }

        @Override
        public Show[] newArray(int size) {
            return new Show[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(address);
        dest.writeInt(placement);
    }
}

enum Safety implements Parcelable{
    NICE,PET,WILD;
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeInt(ordinal());
    }

    public static final Parcelable.Creator<Safety> CREATOR = new Parcelable.Creator<Safety>() {
        @Override
        public Safety createFromParcel(final Parcel source) {
            return Safety.values()[source.readInt()];
        }

        @Override
        public Safety[] newArray(final int size) {
            return new Safety[size];
        }
    };
}

0 个答案:

没有答案