Android 类 Parcelable 与 ArrayList

标签 android class arraylist parcelable

我有一个 android 项目,我有一个类。在该类中是 ArrayList<Choices> .我将获取一些 XML,将其解析出来,然后从中生成对象,然后将其传递给另一个 Activity 。我为此选择 Parcelable。

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

}

感谢您的帮助!

最佳答案

如果您需要在 Activity 之间传递 ArrayList,那么我也会实现 Parcelable,因为我猜没有其他方法。 但是,我认为您不需要那么多 getter 和 setter。这是实现 ParcelableQuestion 类:

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

    };
}

关于Android 类 Parcelable 与 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446359/

相关文章:

android - Firebase Analytics 禁用受众国家/地区跟踪

c++ - 数据类型 inst 转换正确吗?

css - 我可以用类覆盖 CSS #ID 吗?

java - 将数据从多个数组列表传输到多维数组

java - 在处理 ArrayList 时,我可以用什么来替代 charAt?

java - 显示列表后如何删除方括号?

java - 安卓随机数

android - 同时购买应用程序项目中的多种消耗品

android - 如何获取按钮的背景id

javascript - 在类中使用 async await 来分配属性但返回 null