具有ArrayList的Android类Parcelable

时间:2014-03-17 03:19:55

标签: android class arraylist parcelable

我有一个android项目,我有一个类。在那个班级是ArrayList<Choices>。我将获得一些XML,解析它,然后从中创建对象,我将传递给另一个活动。我正在为此选择Parcelable。

Parcelable是个不错的选择吗?我做的一切都正确吗?我对Parcelable真的不熟悉。我的ArrayList是我在这个类中创建的另一个类。它是否会正确地将对象的ArrayList传递给Parcel,而不会扩展Parcelable和stuff?

import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.os.ParcelableCompat;

public class Question implements Parcelable{


String id;
String text;
String image;
ArrayList<Choices> CHOICES;


public Question(String id, String text, String image) {
    super();
    this.id = id;
    this.text = text;
    this.image = image;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

@Override
public String toString() {
    return "Question [id=" + id + ", text=" + text + ", image=" + image
            + "]";
}




// Answer Choices class
class Choices {

    boolean isCorrect;
    String choice;

    public Choices(boolean isCorrect, String choice) {
        this.isCorrect = isCorrect;
        this.choice = choice;
    }

    public String getChoice() {
        return choice;
    }

    public boolean getIsCorrect() {
        return isCorrect;
    }

    @Override
    public String toString() {
        return "Choices [isCorrect=" + isCorrect + ", choice=" + choice
                + "]";
    }

}


public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {

    @Override
    public Question createFromParcel(Parcel in) {
        return new Question(in);
    }

    @Override
    public Question[] newArray(int size) {
        return new Question[size];
    }

};

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(id);
    dest.writeString(text);
    dest.writeString(image);
    dest.writeList(CHOICES);

}

private Question(Parcel in) {
    this.id = in.readString();
    this.text = in.readString();
    this.image = in.readString();
    this.CHOICES = in.readArrayList(Choices.class.getClassLoader());
}

}

感谢您的帮助!

4 个答案:

答案 0 :(得分:32)

如果您需要在活动之间传递ArrayList,那么我也会实施Parcelable,因为我猜没有别的方法。 但是,我认为你不需要那么多的吸气剂和制定者。这是您的Question类,它实现了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];
        }

    };
}

答案 1 :(得分:4)

你几乎可以,但不完全正确。 Question类看起来几乎正确Parcelable。唯一不起作用的是分配选择数组。

有两种方法可以做到:

  1. 选择可以选择。您必须添加所有必需的方法和CREATOR。因为Android知道如何包裹Parcelables的ArrayLists,所以这将有效。
  2. 对parcelling the Question进行parceling数组选择。为此,您可能会将数组的大小推入Parcel,然后循环选择,推送它们的值。另一方面,您首先读取计数,然后读取每个选择的值,创建每个选项并将其推入新问题。

答案 2 :(得分:2)

使用:

in.createTypedArrayList(Product.CREATOR)

在将Parable对象作为param的构造函数中。

在writeToParcel方法中使用dest.writeTypedList(product);

答案 3 :(得分:2)

为“ Choices”创建一个新的Java文件并实现“ Parcelable”。如果不实现可包裹性,则将获得运行时异常(无法元帅)。因此,请使用以下代码:

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

如上述@ G.Blake的回答中所述,您需要将Choices设置为Parcelable,而Android知道如何打包Parcelables的ArrayLists