使用Parcelable接口在模型中传输对象

时间:2017-04-25 19:11:48

标签: serialization model-view-controller parcelable

我尝试使用Parcelable接口在活动之间传输对象,但是我遇到了问题。如果我在下一个活动中获取此对象模型,则User和Place字段等于null,但其他字段不为null。我可以使用Parcelable在模型PizzaRestraunt中的下一个活动中传输用户和地点字段,或者我应该使用接口序列化?模型User和Place实现接口Parcelable。

public class PizzaRestraunt implements Parcelable {
private String id;
private String restrauntName;
private User user;
private Place place;
private String phone;
private String twitter;
private String facebook;
private int distance;
private String adress;
private String category;

public String getId() {
    return id;
}

public String getRestrauntName() {
    return restrauntName;
}

public String getPhone() {
    return phone;
}

public String getTwitter() {
    return twitter;
}

public String getFacebook() {
    return facebook;
}

public int getDistance() {
    return distance;
}
public User getUser() {
    return user;
}
public Place getPlace() {
    return place;
}

public String getAdress() {
    return adress;
}

public String getCategory() {
    return category;
}

public void setId(String id) {
    this.id = id;
}

public void setRestrauntName(String restrauntName) {
    this.restrauntName = restrauntName;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public void setUser(User user) {
    this.user = user;
}
public void setPlace(Place place) {
    this.place = place;
}
public void setTwitter(String twitter) {
    this.twitter = twitter;
}

public void setFacebook(String facebook) {
    this.facebook = facebook;
}

public void setDistance(int distance) {
    this.distance = distance;
}

public void setAdress(String adress) {
    this.adress = adress;
}

public void setCategory(String category) {
    this.category = category;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(id);
    parcel.writeString(facebook);
    parcel.writeString(twitter);
    parcel.writeString(category);
    parcel.writeString(phone);
    parcel.writeString(adress);
    parcel.writeString(restrauntName);
    parcel.writeInt(distance);

}

public static final Parcelable.Creator<PizzaRestraunt> CREATOR =
        new Parcelable.Creator<PizzaRestraunt>() {

            public PizzaRestraunt createFromParcel(Parcel in) {
                return new PizzaRestraunt(in);
            }

            public PizzaRestraunt[] newArray(int size) {
                return new PizzaRestraunt[size];
            }
        };

private PizzaRestraunt(Parcel parcel) {
    id = parcel.readString();
    restrauntName = parcel.readString();
    phone = parcel.readString();
    category = parcel.readString();
    facebook = parcel.readString();
    twitter = parcel.readString();
    adress = parcel.readString();
    distance = parcel.readInt();
}

public PizzaRestraunt() {

}

1 个答案:

答案 0 :(得分:0)

因此,使用接口Parcelable传输到引用数据类型需要使用下一个方法:

 @Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(user, 1);
    dest.writeParcelable(place, 2);
}

private ProjectModel(Parcel parcel) {
    user = parcel.readParcelable(User.class.getClassLoader());
    place = parcel.readParcelable(Place.class.getClassLoader());
}