带有 ArrayList 的 Android 类 Parcelable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22446359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Class Parcelable with ArrayList
提问by Kenny
I have an android project where I have a class. In that class is an ArrayList<Choices>
. I will be getting some XML, parsing it out, then making objects out of it which I will be passing to another activity. I'm choosing Parcelable for this.
我有一个 android 项目,我在那里上课。在那个班级是一个ArrayList<Choices>
. 我将获取一些 XML,将其解析出来,然后从中创建对象,然后将其传递给另一个活动。我为此选择 Parcelable。
Is Parcelable a good choice? Am I doing everything correctly? I'm not familiar really with Parcelable. My ArrayList is of another class that I made within this class. Will it properly pass that ArrayList of objects to the Parcel with it not extending Parcelable and stuff?
Parcelable 是一个不错的选择吗?我做的一切都正确吗?我真的不熟悉 Parcelable。我的 ArrayList 是我在这个类中创建的另一个类。它会正确地将对象的 ArrayList 传递给 Parcel 而不扩展 Parcelable 之类的东西吗?
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());
}
}
Thanks for any help!
谢谢你的帮助!
回答by Miro Markaravanes
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
:
如果您需要ArrayList
在活动之间传递一个,那么我也会执行Parcelable
,因为我想没有其他方法可以解决。但是,我认为您不需要那么多的 getter 和 setter。这是您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];
}
};
}
回答by G. Blake Meike
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.
你几乎拥有它,但不完全是,对。Question 类看起来几乎正确 Parcelable。唯一行不通的是对Choices 数组进行分割。
There are two ways that you could do it:
有两种方法可以做到:
- 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.
- 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.
- 使选择可打包。您必须添加所有必需的方法和 CREATOR。因为 Android 知道如何打包 Parcelables 的 ArrayLists,这将起作用。
- 将选择数组打包作为打包问题的一部分。为此,您可能会将数组的大小推送到 Parcel,然后遍历 Choices,推送它们的值。另一方面,您首先读取计数,然后读取每个选项的值,创建每个选项并将其推送到新问题中。
回答by Derrick
Use:
用:
in.createTypedArrayList(Product.CREATOR)
In the constructor that takes a Parable object as a param.
在将 Parable 对象作为参数的构造函数中。
In the writeToParcel method use dest.writeTypedList(product);
在 writeToParcel 方法中使用 dest.writeTypedList(product);
回答by Hemant Shori
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 :
为“Choices”创建一个新的java文件并实现“Parcelable”。如果您不实施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);
}
}
As mentioned in above answer by @G.Blake you need to make Choices Parcelable and Android knows how to parcel ArrayLists of Parcelables
正如@G.Blake 在上面的回答中提到的,你需要让 Choices Parcelable 和 Android 知道如何打包 Parcelables 的 ArrayLists