如何用另一个对象的数组分隔对象

时间:2014-09-02 09:47:42

标签: android arrays object parcelable

我正在尝试使用Parcelable Object在活动之间传递数据。使用主要变量我没有问题,但现在我需要传递另一个对象的数组,而我不知道该怎么做。我正在寻找一些例子,但我没有找到与我的问题相似的案例。

这是主要对象的源代码:

public class Quiz implements Parcelable {

    int event_id;
    String name;
    String business_unit;
    int functional_area_business;    // 0 unchecked, 1 checked
    int functional_area_support;    // 0 unchecked, 1 checked
    Answer [] list_answers;

    //GETTERS AND SETTERS
    ...
    //


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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(event_id);
        dest.writeString(name);
        dest.writeString(business_unit);
        dest.writeInt(functional_area_business);
        dest.writeInt(functional_area_support);
        dest.writeArray(list_answers);
    }

    private Quiz(Parcel in) {
        event_id = in.readInt();
        name = in.readString();
        business_unit = in.readString();
        functional_area_business = in.readInt();
        functional_area_support = in.readInt();
    }
}

另一类是下一个。

public class Answer {

    int answer_id;
    String value;

}

如果有人能告诉我需要修改的内容,我将非常感激。

1 个答案:

答案 0 :(得分:1)

使用ArrayList而不是Array并使Answer类Parcelable然后将代码更新为以下内容:

  public class Quiz implements Parcelable {

        int event_id;
        String name;
        String business_unit;
        int functional_area_business;    // 0 unchecked, 1 checked
        int functional_area_support;    // 0 unchecked, 1 checked
        List<Answer >  list_answers;

        //GETTERS AND SETTERS
        ...
        //


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

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

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

    @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(event_id);
            dest.writeString(name);
            dest.writeString(business_unit);
            dest.writeInt(functional_area_business);
            dest.writeInt(functional_area_support);
            dest.writeTypedList(list_answers);
        }

    private Quiz(Parcel in) {
            event_id = in.readInt();
            name = in.readString();
            business_unit = in.readString();
            functional_area_business = in.readInt();
            functional_area_support = in.readInt();
            list_answers = new ArrayList<Answer>();
            in.readTypedList(list_answers , Answer.CREATOR);
        }
    }

这是Parcelable答案:

    public class Answer implements Parcelable  {

        int answer_id;
        String value;



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

            public Answer[] newArray(int size) {
                return new Answer[size];
            }
        };
       @Override
            public int describeContents() {
                return 0;
            }

        @Override
            public void writeToParcel(Parcel dest, int flags) {
                dest.writeInt(answer_id);
                dest.writeString(value);

    }
    private Answer(Parcel in) {
                answer_id = in.readInt();
                value = in.readString();
    }

    }