How to parse an object with an array of another object

I am trying to make a Parcelable Object to pass data between activities. I have no problem with primary variables, but now I need to pass an array of another object and I don't know how. I'm looking for some examples, but I haven't found a similar case for my problem.

This is my source code for the main 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();
    }
}

      

Another class is next.

public class Answer {

    int answer_id;
    String value;

}

      

If someone can tell me what I need to change, I would be grateful.

+3


source to share


1 answer


Use an ArrayList instead of an array and create a Parseable response class and then update your code as follows:

  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);
        }
    }

      



And this is Parceable's answer:

    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();
    }

    }

      

+1


source







All Articles