java - 将多个对象写入包

标签 java android parcelable

我正在尝试将枚举“状态”保存到实现 Parcelable 的自定义类中。我在网上找到了如何在一个实现 Parcelable 的类中保存字符串、整数或枚举,但没有找到如何同时保存这三样东西。如果解决方案很明显,我很抱歉,但我就是想不出来。

这是我的枚举的样子:

public enum Status {
    INITIALIZED, UPDATED, DELETED
}

这就是我到目前为止所拥有的:

public class Recipe implements Parcelable{
private String id;//this should be an int, same problem
private String recipeName;
private String recipePreperation;
private Status status;
private final static int MAX_PREVIEW = 50;

public Recipe(int parId, String parRecipeName, String parRecipePreperation) {
    this.id = "" + parId;
    this.recipeName = parRecipeName;
    this.recipePreperation = parRecipePreperation;
    this.status = Status.INITIALIZED;
}

public Recipe(Parcel in){
    String[] data = new String[4];

    in.readStringArray(data);
    this.id = data [0];
    this.recipeName = data[1];
    this.recipePreperation = data[2];
    this.status = data[3];//what I intend to do, I know this is wrong
}

public int GetId() {
    return Integer.parseInt(id);
}

public String GetRecipeName() {
    return this.recipeName;
}

public void SetRecipeName(String parRecipeName) {
    this.recipeName = parRecipeName;
}

public String GetRecipePreperation() {
    return this.recipePreperation;
}

public void SetRecipePreperation(String parRecipePreperation) {
    this.recipePreperation = parRecipePreperation;
}

public Status GetStatus() {
    return this.status;
}

public void SetStatus(Status parStatus) {
    this.status = parStatus;
}

public String toString() {
    String recipe = this.recipeName + "\n" + this.recipePreperation;
    String returnString;
    int maxLength = MAX_PREVIEW;

    if (recipe.length() > maxLength) {
        returnString = recipe.substring(0, maxLength - 3) + "...";
    } else {
        returnString = recipe;
    }

    return returnString;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int arg1) {
    dest.writeStringArray(new String [] {
            this.id,
            this.recipeName,
            this.recipePreperation,
            this.status//what I intend to do, I know this is wrong
    });
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public Recipe createFromParcel(Parcel in) {
        return new Recipe(in);
    }

    public Recipe[] newArray(int size) {
        return new Recipe[size];
    }
};
}

如何将 int、字符串数组和枚举保存到实现 Parcelable 的类中,以便它可以 writeToParcel()?

最佳答案

无需对字符串数组进行读写。只需写入每个字符串,最后将状态写入 Serialized 即可。这就是我修复它的方法。

public Recipe(Parcel in){
    this.id = in.readString();
    this.recipeName = in.readString();
    this.recipePreperation = in.readString();
    this.status = (Status) in.readSerializable();
}

public void writeToParcel(Parcel dest, int arg1) {
    dest.writeString(this.id);
    dest.writeString(this.recipeName);
    dest.writeString(this.recipePreperation);
    dest.writeSerializable(this.status);
}

关于java - 将多个对象写入包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30674144/

相关文章:

java - 使用biojava将文本文件读入JTable的特定列的步骤

java - 使用 Spring Integration 移动文件对象

android - 如何解决 : "cannot find getter for attribute ' android:text'"when implementing two-way data binding with custom view?

php - Android 上的实时数据库与 Google map

java - 为什么 String.split 需要转义管道分隔符?

java - Kotlin 无法识别 Android XML 元素的 ID

java - 在android中使用volley发送json数组将mysql中的多条记录添加到php脚本

java - Android抽象parcelable类

Android Parcelable 与 JsonObject

android - YouTube Android 播放器 API 在新的 YouTube 版本中抛出 "BadParcelableException ClassNotFoundException when unmarshalling: asc"