我在我的另一个实现Parcelable的Venue对象中有一个Location对象。如何在writeToParcel方法实现中正确序列化?所以这里有一些代码:
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
}
}
答案 0 :(得分:10)
这里有一个片段,如何将parcelable对象序列化到您自己的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 */
}
答案 1 :(得分:0)
location = in.readParcelable(Location.class.getClassLoader());
也有效。