запись нескольких объектов в посылку


Я пытаюсь сохранить перечисление 'Status' в пользовательский класс, реализующий parcelable. Я нашел в интернете, как я могу сохранить строки, ints или перечисления в одном классе, который реализует 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()?

1 2

1 ответ:

Нет необходимости читать и записывать в/из массива строк. Просто напишите каждую строку и, наконец, статус как Serializable. Вот как я это исправляю.

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