没有关于Parcelable对象的数据发送到其他parcelables里面的活动

时间:2014-09-09 09:26:29

标签: android parcelable

我正在做一个需要通过活动传递对象的应用程序,所以为了达到这个目的,我已经制作了3个可分类的类。

结构如下:

> List of Quiz:
>  - 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<Quiz> list_quizs;
>          |
>          \
>           -> Quiz:
>               - int id;
>               - String title;
>               - String description;
>               - int speaker_id;
>               - String speaker_name;
>               - List<Answer> list_answers;
>                        |
>                        \
>                         -> Answer:
>                             - int answer_id;
>                             - String title;
>                             - int order;
>                             - int type;
>                             - String value;
>                             - int quiz_parent;

正如你所看到的,List_Quiz有一些变量和一个Quiz(类)列表,Quiz是相同的,一些变量和一个Answers列表(另一个类)。

我不知道当我通过意图发送主对象“List_Quiz”时,发送的数据是带有Quiz和Answers的List_Quiz的完整树,还是只存储在第一个parcelable类List_Quiz中的数据,因为我不知道从接收可分配对象的活动中的答案中获取任何数据。

我的可分类课程是:

LIST_QUIZ

public class List_Quizs 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<Quiz> list_quizs; // MyClass should implement Parcelable properly

    // ==================== Parcelable ====================
    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(event_id);
        out.writeString(name);
        out.writeString(business_unit);
        out.writeInt(functional_area_business);
        out.writeInt(functional_area_support);
        out.writeList(list_quizs);
    }

    public List_Quizs() {
    }

    private List_Quizs(Parcel in) {
        event_id = in.readInt();
        name = in.readString();
        business_unit = in.readString();
        functional_area_business = in.readInt();
        functional_area_support = in.readInt();
        list_quizs = new ArrayList<Quiz>();
        in.readList(list_quizs, List_Quizs.class.getClassLoader());
    }

    public static final Parcelable.Creator<List_Quizs> CREATOR = new Parcelable.Creator<List_Quizs>() {
        public List_Quizs createFromParcel(Parcel in) {
            return new List_Quizs(in);
        }
        public List_Quizs[] newArray(int size) {
            return new List_Quizs[size];
        }
    };

}

测验

public class Quiz implements Parcelable {

    int id;
    String title;
    String description;
    int speaker_id;
    String speaker_name;
    List<Answer> list_answers;

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(title);
        dest.writeString(description);
        dest.writeInt(speaker_id);
        dest.writeString(speaker_name);
        dest.writeList(list_answers);
    }

    public Quiz(Parcel in) {

        id = in.readInt();
        title = in.readString();
        description = in.readString();
        speaker_id = in.readInt();
        speaker_name = in.readString();
        list_answers = new ArrayList<Answer>();
        in.readList(list_answers , Quiz.class.getClassLoader());
    }

    public Quiz(){

    }

    //GETTER, 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];
        }
    };
}

ANSWER

public class Answer implements Parcelable {

    int answer_id;
    String title;
    int order;
    int type;
    String value;
    int quiz_parent;

    //GETTERS, SETTERS        

    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(title);
        dest.writeInt(order);
        dest.writeInt(type);
        dest.writeString(value);
        dest.writeInt(quiz_parent);
    }

    public Answer(Parcel in) {
        answer_id = in.readInt();
        title = in.readString();
        order = in.readInt();
        type = in.readInt();
        value = in.readString();
        quiz_parent = in.readInt();
    }

    public Answer() {
    }
}

1 个答案:

答案 0 :(得分:0)

你有

list_answers = new ArrayList<Answer>();
in.readList(list_answers , Quiz.class.getClassLoader());
你的测验课上的

(糟糕的复制粘贴?:p)。它应该是

list_answers = new ArrayList<Answer>();
in.readList(list_answers , Answer.class.getClassLoader());