Android Class Parcelable with ArrayList

后端 未结 4 1093
太阳男子
太阳男子 2020-11-30 01:42

I have an android project where I have a class. In that class is an ArrayList. I will be getting some XML, parsing it out, then making objects ou

相关标签:
4条回答
  • 2020-11-30 02:19

    You have it almost, but not quite, right. The Question class looks nearly correctly Parcelable. The only thing that won't work is parcelling the array of Choices.

    There are two ways that you could do it:

    1. Make Choices Parcelable. You will have to add all the required methods, and the CREATOR. Because Android knows how to parcel ArrayLists of Parcelables, this will work.
    2. Make parceling the Array of Choices part of parcelling the Question. To do this, you'd probably push the size of the array into the Parcel, and then loop over the Choices, pushing their values. On the other end, you'd read the count first, and then read the values for each Choice, creating each and pushing it into the new Question.
    0 讨论(0)
  • 2020-11-30 02:36

    Use:

    in.createTypedArrayList(Product.CREATOR)
    

    In the constructor that takes a Parable object as a param.

    In the writeToParcel method use dest.writeTypedList(product);

    0 讨论(0)
  • 2020-11-30 02:39

    Create a new java file for "Choices" and implement "Parcelable". If you do not implement parcelable you will get run-time exception (Unable to Marshal). So use the code below :

        public class Choices implements Parcelable{
    
            boolean isCorrect;
            String choice;
    
            public Choices(boolean isCorrect, String choice) {
                this.isCorrect = isCorrect;
                this.choice = choice;
            }
            //Create getters and setters 
    
            protected Choices(Parcel in) {
                isCorrect = in.readByte() != 0;
                choice = in.readString();
            }
    
            public static final Creator<Choices> CREATOR = new Creator<Choices>() {
                @Override
                public Choices createFromParcel(Parcel in) {
                    return new Choices(in);
                }
    
                @Override
                public Choices[] newArray(int size) {
                    return new Choices[size];
                }
            };
    
            @Override
            public String toString() {
                return "Choices [isCorrect=" + isCorrect + ", choice=" + choice
                        + "]";
            }
    
            @Override
            public int describeContents() {
                return 0;
            }
    
            @Override
            public void writeToParcel(Parcel dest, int flags) {
                dest.writeByte((byte) (isCorrect ? 1 : 0));
                dest.writeString(choice);
            }
        }
    

    As mentioned in above answer by @G.Blake you need to make Choices Parcelable and Android knows how to parcel ArrayLists of Parcelables

    0 讨论(0)
  • 2020-11-30 02:44

    If you need to pass an ArrayList between activities, then I'd go with implementing Parcelable also, as there is no other way around I guess. However I don't think you will need that much of getters and setters. Here is your Question class which implements Parcelable:

    public class Question implements Parcelable {
        public String id;
        public String text;
        public String image;
        public ArrayList<Choice> choices;
    
    
        /**
         * Constructs a Question from values
         */
        public Question (String id, String text, String image, ArrayList<Choice> choices) {
            this.id = id;
            this.text = text;
            this.image = image;
            this.choices = choices;
        }
    
        /**
         * Constructs a Question from a Parcel
         * @param parcel Source Parcel
         */
        public Question (Parcel parcel) {
            this.id = parcel.readString();
            this.text = parcel.readString();
            this.image = parcel.readString();
            this.choices = parcel.readArrayList(null);
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        // Required method to write to Parcel
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(id);
            dest.writeString(text);
            dest.writeString(image);
            dest.writeList(choices);
        }
    
        // Method to recreate a Question from a Parcel
        public static Creator<Question> CREATOR = new Creator<Question>() {
    
            @Override
            public Question createFromParcel(Parcel source) {
                return new Question(source);
            }
    
            @Override
            public Question[] newArray(int size) {
                return new Question[size];
            }
    
        };
    }
    
    0 讨论(0)
提交回复
热议问题