Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put a Location object in Parcelable

Tags:

java

android

I have a Location object in one of my other Venue object that implements Parcelable. How do I serialize this correctly in my writeToParcel method implementation? So here's some code:

public class Venue implements Parcelable{
    public String id;
    public String name;
    public String address;
    public Location location;
    public String city;
    public String state;
    public String country;
    public String postalCode;

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel desc, int flags) {
        desc.writeString(id);
        desc.writeString(state);
        desc.writeString(name);
        desc.writeString(address);
        desc.writeString(postalCode);
        desc.writeString(country);
        desc.writeString(city);
        //I am still missing on a way to serialize my Location object here
    }
}
like image 949
adit Avatar asked Nov 13 '11 15:11

adit


1 Answers

Here your have a snippet how to serialize parcable objects into your own parcel.

Location location;

public void writeToParcel(Parcel desc, int flags) {
    location.writeToParcel(desc, flags);
    /* do your other parcel stuff here */
}

public void readFromParcel(Parcel in) {
    location=Location.CREATOR.createFromParcel(in);
    /* do your other parcel stuff here */
}
like image 86
H9kDroid Avatar answered Oct 20 '22 17:10

H9kDroid