Serialization 使用接口Parcelable传递模型中的对象
我尝试使用interface Parcelable在活动之间传输对象,但遇到了一个问题。若我在下一个活动中得到这个对象模型,User和Place字段等于null,但其他字段不为null。我可以使用Parcelable传输用户字段并将其放置在模型Pizza中的下一个活动中,还是应该使用接口序列化?模型用户和位置实现界面可包裹Serialization 使用接口Parcelable传递模型中的对象,serialization,model-view-controller,parcelable,Serialization,Model View Controller,Parcelable,我尝试使用interface Parcelable在活动之间传输对象,但遇到了一个问题。若我在下一个活动中得到这个对象模型,User和Place字段等于null,但其他字段不为null。我可以使用Parcelable传输用户字段并将其放置在模型Pizza中的下一个活动中,还是应该使用接口序列化?模型用户和位置实现界面可包裹 public class PizzaRestraunt implements Parcelable { private String id; private String r
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() {
}
公共类比萨饼可包裹{
私有字符串id;
私有字符串重新启动名称;
私人用户;
私人场所;
私人电话;
私有字符串twitter;
私有字符串facebook;
私有整数距离;
私有字符串地址;
私有字符串类别;
公共字符串getId(){
返回id;
}
公共字符串getRestrauntName(){
返回重新启动名称;
}
公共字符串getPhone(){
回电话;
}
公共字符串getTwitter(){
返回推特;
}
公共字符串getFacebook(){
返回facebook;
}
公共int getDistance(){
返回距离;
}
公共用户getUser(){
返回用户;
}
公共场所{
返回地点;
}
公共字符串getAddress(){
返回地址;
}
公共字符串getCategory(){
退货类别;
}
公共无效集合id(字符串id){
this.id=id;
}
public void setrestaruntname(字符串restaruntname){
this.restrauntName=restrauntName;
}
公用无效设置电话(字符串电话){
this.phone=电话;
}
公共void setUser(用户){
this.user=用户;
}
公共空间设置地点(地点){
这个地方=地方;
}
公共void setTwitter(字符串twitter){
this.twitter=twitter;
}
public void setFacebook(字符串facebook){
this.facebook=facebook;
}
公共空间设置距离(整数距离){
这个距离=距离;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
公共无效集合类别(字符串类别){
this.category=类别;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效书面包裹(包裹,内部i){
包裹。书面记录(id);
parcel.writeString(facebook);
包裹。writeString(推特);
包裹。书面记录(类别);
包裹。书面记录(电话);
包裹。书面记录(地址);
包裹写入(重新启动名称);
包裹书写(距离);
}
公共静态最终包裹。创建者=
新建Parcelable.Creator(){
公共比萨饼从包裹(包裹中){
归还新的比萨饼(in);
}
公共比萨饼新阵列(整数大小){
返回新的比萨饼[尺寸];
}
};
私人比萨饼(包裹){
id=parcel.readString();
restrauntName=parcel.readString();
phone=packet.readString();
类别=地块。readString();
facebook=parcel.readString();
twitter=parcel.readString();
adress=地块。readString();
距离=parcel.readInt();
}
公共披萨店(){
}
因此,使用接口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());
}