How to send a parcelable from an object with list?

0

I'm trying to send an object to a activity using parcelable , it happens that the object is sent but the lists that are inside it arrive empty ...

Class containing lists: (I tried to solve the problem using the Pareto generator from Android Studio, it generated this and the lines I commented but it did not work, so I added what you're commenting with #)

public class Recipes implements Parcelable {

    public static final String RECIPES_URL = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/5907926b_baking/baking.json";

    private int id;
    private String name;
    private int servings;
    private String image;
    private List<Ingredients> ingredientsList;
    private List<Steps> stepsList;
    public static final String PARCEABLE_KEY = "RECIPES_KEY";

    protected Recipes(Parcel in) {
        id = in.readInt();
        name = in.readString();
        servings = in.readInt();
        image = in.readString();
        ingredientsList = new ArrayList<>(); //#
        in.readList(ingredientsList, List.class.getClassLoader()); //#
        stepsList = new ArrayList<>(); //#
        in.readList(stepsList, List.class.getClassLoader()); //#
        /*ingredientsList = in.createTypedArrayList(Ingredients.CREATOR);
        stepsList = in.createTypedArrayList(Steps.CREATOR);*/
    }

    public Recipes(){}

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

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

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getServings() {
        return servings;
    }

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

    public String getImage() {
        return image;
    }

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

    public List<Ingredients> getIngredientsList() {
        return ingredientsList;
    }

    public void setIngredientsList(List<Ingredients> ingredientsList) {
        this.ingredientsList = ingredientsList;
    }

    public List<Steps> getStepsList() {
        return stepsList;
    }

    public void setStepsList(List<Steps> stepsList) {
        this.stepsList = stepsList;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
        dest.writeInt(servings);
        dest.writeString(image);
        dest.writeTypedList(ingredientsList);
        dest.writeTypedList(stepsList);
    }
}

The objects in the list: (I implemented parcelables in them to try to solve the problem)

public class Ingredients extends Recipes implements Parcelable {
    private int quantity;
    private String measure;
    private String ingredient;

    protected Ingredients(Parcel in) {
        super(in);
    }

    public Ingredients() {}

    public int getQuantity() {
        return quantity;
    }

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

    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 class Steps extends Recipes implements Parcelable {
    private int id;
    private String shortDescription;
    private String description;
    private String videoUrl;
    private String thumbnailUrl;

    public Steps() {}

    @Override
    public int getId() {
        return id;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    public String getShortDescription() {
        return shortDescription;
    }

    public void setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    public String getThumbnailUrl() {
        return thumbnailUrl;
    }

    public void setThumbnailUrl(String thumbnailUrl) {
        this.thumbnailUrl = thumbnailUrl;
    }
}

I can not use serializable , it has to be parcelable !

UPDATE: When debugging, I noticed that ingredientsList always comes with size 38, but everything is empty, whereas stepsList comes with size 0 ... No matter which item I select! (by clicking on an item in a recycleview, it is sent to another actvity object of this item, which in this case is Recipes)

    
asked by anonymous 13.05.2017 / 00:17

1 answer

1

I solved by removing the parts with comment and adding in.readTypedList(ingredientsList, Ingredients.CREATOR); in the constructor

public class Recipes implements Parcelable {

    public static final String RECIPES_URL = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/5907926b_baking/baking.json";

    private int id;
    private String name;
    private int servings;
    private String image;
    private List<Ingredients> ingredientsList;
    private List<Steps> stepsList;
    public static final String PARCEABLE_KEY = "RECIPES_KEY";

    protected Recipes(Parcel in) {
        id = in.readInt();
        name = in.readString();
        servings = in.readInt();
        image = in.readString();
        ingredientsList = new ArrayList<>();
        in.readTypedList(ingredientsList, Ingredients.CREATOR);
stepsList = new ArrayList<>();
        in.readTypedList(stepsList, Steps.CREATOR);
    }

    public Recipes(){}

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

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

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getServings() {
        return servings;
    }

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

    public String getImage() {
        return image;
    }

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

    public List<Ingredients> getIngredientsList() {
        return ingredientsList;
    }

    public void setIngredientsList(List<Ingredients> ingredientsList) {
        this.ingredientsList = ingredientsList;
    }

    public List<Steps> getStepsList() {
        return stepsList;
    }

    public void setStepsList(List<Steps> stepsList) {
        this.stepsList = stepsList;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
        dest.writeInt(servings);
        dest.writeString(image);
        dest.writeTypedList(ingredientsList);
        dest.writeTypedList(stepsList);
    }
}
    
13.05.2017 / 02:25