I'm doing an app that need to pass an object through activities, so I've done to achieve this 3 classes parcelables.
The structure is the next:
> 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;
As you can see, List_Quiz has some variables and a list of Quiz(class), Quiz is the same, some variables and a list of Answers(another class).
I don't know If when I send the main object "List_Quiz" through intents the data sended is the complete tree of List_Quiz with Quizs and with Answers, or only the data content in the first parcelable class List_Quiz, because I don't get any data from answers in the activity that receives the parcelable object.
My Parcelable classes are this:
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];
}
};
}
QUIZ
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() {
}
}
0 comments:
Post a Comment