2015-06-05 3 views
2

Я пытаюсь сохранить enum 'Status' в пользовательский класс, который реализует parcelable. Я нашел онлайн, как я могу сохранить строки, ints или перечисления в одном классе, который реализует логически, но не как я могу сохранить эти три вещи сразу. Мне жаль, если решение очевидно, но я просто не могу понять.Запись нескольких объектов на партию

Вот что мое перечисление выглядит следующим образом:

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

Как сохранить в ИНТ массив строк и перечисление в класс, который реализует аргумент, так что он может писатьToParcel()?

ответ

3

Нет необходимости читать и писать в/из массива строк. Просто напишите каждую строку и, наконец, статус 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); 
} 
+0

спасибо! Это то, что я искал. Он также выглядит намного чище, чем массив строк :) – Janneman96

+0

@ Janneman96 Добро пожаловать. – omainegra

Смежные вопросы