Home » Android » android – Making ArrayList of custom objects parcelable

android – Making ArrayList of custom objects parcelable

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have two classes which create Objects I need to pass through intents, I am currently attempting to implement Parcelable to do this.

import android.os.Parcel;
import android.os.Parcelable;

public class Item implements Parcelable {

    private String name;
    private double price;
    private int buyerCount = 0;

    public Item(String name, double price) {
        this.name = name;
        this.price = price;
    }

    private Item(Parcel in) {
        name = in.readString();
        price = in.readDouble();
    }

    public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>() {
        public Item createFromParcel(Parcel in) {
            return new Item(in);
        }

        public Item[] newArray(int size) {
            return new Item[size];
        }
    };

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

    public double getPrice() {
        return price;
    }

    public int getBuyerCount() {
        return buyerCount;
    }

    public void incrementBuyerCount() {
        buyerCount += 1;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeDouble(price);
    }
}

From testing so far my Item object seems to transfer information correctly when I bundle it.

However my second class Diner contains an ArrayList of objects Item and I’m unsure how to correctly implement Parcelable in this class, specifically in the parts where I have commented. I see there is a readParsableArray() method but nothing for ArrayLists.

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;

public class Diner implements Parcelable {

    private String name;
    private ArrayList<Item> itemList = new ArrayList<Item>();

    public Diner(String name) {
        this.name = name;
    }

    private Diner(Parcel in) {
        name = in.readString();
        // How to read in ArrayList itemList of item objects?
        itemList = in.readParcelableArray();
    }

    public static final Parcelable.Creator<Diner> CREATOR = new Parcelable.Creator<Diner>() {
        public Diner createFromParcel(Parcel in) {
            return new Diner(in);
        }

        public Diner[] newArray(int size) {
            return new Diner[size];
        }
    };

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        // How to write ArrayList itemList of item objects?
        dest.writeParcelableArray(itemList);
    }

    public void addItem(Item foodItem) {
        itemList.add(foodItem);
        foodItem.incrementBuyerCount();
    }

    public double getPrice() {
        double total = 0;
        for(Item item : itemList) {
            total += item.getPrice() / item.getBuyerCount();
        }
        return total;
    }

    public String toString() {
        return name;
    }

}
How to&Answers:

Use writeTypedList and readTypedList.

parcel.writeTypedList(yourList);

and

parcel.readTypedList(yourList, YourParcelable.CREATOR);

Notice how you pass in the CREATOR as an argument for reading the list back.

Answer:

I would suggest to use createTypedArrayList, like that:

objectsInList = in.createTypedArrayList(ObjectInList.CREATOR)