自定义对象的可打包实现

时间:2018-07-25 01:50:41

标签: java android parcelable

我有3种POJO类型:包含配料的配方,然后配料包含步骤。以下是我的设置,我正在尝试实现Parcelable,并且无法确定适当的读写方法:

Recipe.Java:

creator(FK)

Ingredients.Java:

public class Recipe implements Parcelable {


protected List<Ingredients> ingredients;

private String id;

private String servings;

private String name;

private String image;

private List<Steps> steps;

protected Recipe(Parcel in) {
    in.createTypedArray(CREATOR.createFromParcel(Ingredients));
    in.createTypedArray(Ingredients.);
    id = in.readString();
    servings = in.readString();
    name = in.readString();
    image = in.readString();
    steps = in.readArrayList(ClassLoader.getSystemClassLoader());
}

public static final Creator<Ingredients> CREATOR = new Creator<Ingredients>() {
    @Override
    public Ingredients createFromParcel(Parcel parcel) {
        return new Ingredients(parcel);
    }

    @Override
    public Ingredients[] newArray(int i) {
        return new Ingredients[0];
    }
}


public static final Creator<Recipe> CREATOR = new Creator<Recipe>() {
    @Override
    public Recipe createFromParcel(Parcel in) {
        return new Recipe(in);
    }

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

public List<Ingredients> getIngredients() {
    return ingredients;
}

public void setIngredients(List<Ingredients> ingredients) {
    this.ingredients = ingredients;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getServings() {
    return servings;
}

public void setServings(String servings) {
    this.servings = servings;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public List<Steps> getSteps() {
    return steps;
}

public void setSteps(List<Steps> steps) {
    this.steps = steps;
}

@Override
public String toString() {
    return "ClassPojo [ingredients = " + ingredients + ", id = " + id + ", servings = " + servings + ", name = " + name + ", image = " + image + ", steps = " + steps + "]";
}

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

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(id);
    parcel.writeString(servings);
    parcel.writeString(name);
    parcel.writeString(image);
    }
 }

Steps.Java:

public class Ingredients {

private String measure;

private String ingredient;

private String quantity;

public Ingredients(Parcel parcel) {
}

public String getMeasure ()
{
    return measure;
}

public void setMeasure (String measure)
{
    this.measure = measure;
}

public String getIngredient ()
{
    return ingredient;
}

public void setIngredient (String ingredient)
{
    this.ingredient = ingredient;
}

public String getQuantity ()
{
    return quantity;
}

public void setQuantity (String quantity)
{
    this.quantity = quantity;
}

@Override
public String toString()
{
    return "ClassPojo [measure = "+measure+", ingredient = "+ingredient+", quantity = "+quantity+"]";
   }
}

}

1 个答案:

答案 0 :(得分:1)

我建议您使用Paperparcel之类的第三方库来减少样板。

用法示例:

@PaperParcel // (1)
public final class User implements Parcelable {
    public static final Creator<User> CREATOR = PaperParcelUser.CREATOR; // (2)

    long id; // (3)
    String firstName; // (3)
    String lastName; // (3)

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

    @Override public void writeToParcel(Parcel dest, int flags) {
        PaperParcelUser.writeToParcel(this, dest, flags); // (4)
    }
}