I have an object in the form
public class Car implements Serializable, Parcelable {
private static final long serialVersionUID = 1L;
String name;
String description;
String brand;
int speed;
int brake;
int asset;
ArrayList<Uri> images;
public Car(String name, String description, String brand, int speed,
int brake, int asset, ArrayList<Uri> images) {
super();
this.name = name;
this.description = description;
this.brand = brand;
this.speed = speed;
this.brake = brake;
this.asset = asset;
this.images = images;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getBrake() {
return brake;
}
public void setBrake(int brake) {
this.brake = brake;
}
public int getAsset() {
return asset;
}
public void setAsset(int asset) {
this.asset = asset;
}
public ArrayList<Uri> getImages() {
return images;
}
public void setImages(ArrayList<Uri> images) {
this.images = images;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + asset;
result = prime * result + brake;
result = prime * result + ((brand == null) ? 0 : brand.hashCode());
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
result = prime * result + ((images == null) ? 0 : images.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + speed;
return result;
}
@Override
public String toString() {
return "Car [name=" + name + ", description=" + description
+ ", brand=" + brand + ", speed=" + speed + ", brake=" + brake
+ ", asset=" + asset + ", images=" + images + "]";
}
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
}
}
But I haven’t understand how to implements
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
}
and
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
that Eclipse have required after the adding of implements Parcelable to the class
on the Android site the sample show also a method
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
What is the right way to make my whole class (and all the nested objects as ArrayList) “Parcelable”?
following the sample I’m arrived to the constructor
public Car(Parcel in) {
name= in.readString();
description= in.readString();
speed=in.readInt();
...
images=in.readArrayList(??????????????????);
}
what is the Class loader to pass in readArrayList(..)?
On the
writeToParcelmethod you should do:The argument
destin thewriteToParcelmethod has a lot of other methods to write other object or raw types.To put other objects that do not have specific methods in the
destobject, you should use the methodswriteParcelableif the object you are trying to write implementsParcelableorwriteSerializableif the object you are trying to write implementsSerializable.After, when you’ll recover your Parceable object, you should use:
You have to read the values from the in object in the same order you wrote them.
The piece of code:
Is necessary to call the
readFromParcelmethod. You should add in your class a constructor to be called from the piece of code above.The constructor should look like:
About the method
describeContents, there is a good answer on Stackoverflow, here.Summarizing, I’ve never seen any case in which this method was used. The method is used if you want to release resources after the parsing of the Parcel, but I didin’t found any good material on the internet explaining it. Chances are that you’ll keep it returning 0.
You can found Android documentation for it here.