Skip to content

Instantly share code, notes, and snippets.

@chengscott
Last active August 29, 2015 14:26
Show Gist options
  • Save chengscott/a0a249cd578426783033 to your computer and use it in GitHub Desktop.
Save chengscott/a0a249cd578426783033 to your computer and use it in GitHub Desktop.
Parcelable example
// LICENSE : https://gist.github.com/52c13856a6d59acaa86f.git
import android.os.Parcel;
import android.os.Parcelable;
public class ShopProxy implements Parcelable {
private static final long serialVersionUID = 1L;
private String name, category, description;
private boolean takeout, authorized;
private double latitude, longtitude;
public ShopProxy() {
name = category = description = null;
takeout = authorized = false;
latitude = longtitude = 0.0;
}
public ShopProxy(Shop shop) {
name = shop.getName();
category = shop.getCategory();
description = shop.getDescription();
takeout = shop.isTakeout();
authorized = shop.isAuthorized();
latitude = shop.getLocation().getLatitude();
longtitude = shop.getLocation().getLongitude();
}
public static final Parcelable.Creator<ShopProxy> CREATOR = new Parcelable.Creator<ShopProxy>() {
public ShopProxy createFromParcel(Parcel in) {
return new ShopProxy(in);
}
public ShopProxy[] newArray(int size) {
return new ShopProxy[size];
}
};
@Override
public int describeContents() {
return 0;
}
public ShopProxy(Parcel in) {
name = in.readString();
category = in.readString();
description = in.readString();
takeout = (in.readByte() != 0);
authorized = (in.readByte() != 0);
latitude = in.readDouble();
longtitude = in.readDouble();
}
// the writing sequence SHOULD correspond to the reading sequence
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(category);
parcel.writeString(description);
parcel.writeByte((byte) (takeout ? 1 : 0));
parcel.writeByte((byte) (authorized ? 1 : 0));
parcel.writeDouble(latitude);
parcel.writeDouble(longtitude);
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public String getDescription() {
return description;
}
public boolean isTakeout() {
return takeout;
}
public boolean isAuthorized() {
return authorized;
}
public double getLatitude() {
return latitude;
}
public double getLongtitude() {
return longtitude;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment