8
votes

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

}
2
Writing parcelable code by hand is error prone. If you are using Android Studio/IntelliJ you could use Parcelabler to do it for you.Emmanuel
Unfortunately this is for a school project assignment, therefore I believe using external plugins is frowned upon.user3371750
Talk you your professor and in a nice way ask him/her if they want you do go the way that developers do it on the real world or not.Emmanuel
You don't need anything these days, Android Studio has a "Add parcelable implementation" (and remove) for you.Martin Marconcini

2 Answers

22
votes

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.

1
votes

I would suggest to use createTypedArrayList, like that:

objectsInList = in.createTypedArrayList(ObjectInList.CREATOR)