How to send a parcelable of a list object?

Asked

Viewed 144 times

0

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

Class containing the lists: (I tried to solve the problem using Android Studio’s parceable generator, it generated this and the lines I commented, but it didn’t work, so I added what is 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 list objects: (I implemented parcelables on 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’t use serializable, has to be parcelable!

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

1 answer

1


I decided to remove the parts with comment and adding in.readTypedList(ingredientsList, Ingredients.CREATOR); in the builder

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.