android - 使用 Android Parcelable 在 Activity 之间传输数据
问题描述
我正在尝试使用 parcelable 在活动之间传输数据我遇到了一些问题。我收到代码部分的错误Expression expected
。putExtraData
调试器说error: cannot find symbol variable ListingsModel
。包含可打包代码的类被称为ListingsModel
构造函数。我似乎无法找到我的错误。我的意图包含在 if 语句中,如果尝试启动活动ReviewActivity.class
并相应地传递所有数据,则该语句应执行。任何帮助将不胜感激。
打算启动 Review 活动类并在 true if 语句上传递数据:
Intent intent = new Intent(MainActivity.this, ReviewActivity.class);
putExtraData("ListingsModel", **ListingsModel**); //error appears here
startActivity(intent);
包含可打包代码的类:
public class ListingsModel implements Parcelable {
public static final int IMAGE_TYPE = 1;
public String title, street, city, state, zip, hours, isOpen, content, image, phone, timestamp;
public int type, rating, ratingCount;
public Double lat, lng;
public ListingsModel(int mtype, String mtitle, Integer mrating, Integer mRatingCount, String mstreet, String mcity, String mstate, String mzip, String mhours, String mIsOpen, String mcontent, String mimage, String mphone, Double mlat, Double mlng, String mtimestamp) {
this.type = mtype;
this.title = mtitle;
this.rating = mrating;
this.ratingCount = mRatingCount;
this.street = mstreet;
this.city = mcity;
this.state = mstate;
this.zip = mzip;
this.hours = mhours;
this.isOpen = mIsOpen;
this.content = mcontent;
this.image = mimage;
this.phone = mphone;
this.lat = mlat;
this.lng = mlng;
this.timestamp = mtimestamp;
}
//write object values to parcel for storage
public void writeToParcel(Parcel dest, int flags){
//write all properties to the parcle
//dest.writeInt(type);
dest.writeString(title);
dest.writeInt(rating);
dest.writeInt(ratingCount);
dest.writeString(street);
dest.writeString(city);
dest.writeString(state);
dest.writeString(zip);
dest.writeString(hours);
dest.writeString(isOpen);
dest.writeString(content);
dest.writeString(image);
dest.writeString(phone);
dest.writeDouble(lat);
dest.writeDouble(lng);
}
//constructor used for parcel
public ListingsModel(Parcel parcel){
//read and set saved values from parcel
title = parcel.readString();
rating = parcel.readInt();
ratingCount = parcel.readInt();
street = parcel.readString();
city = parcel.readString();
state = parcel.readString();
zip = parcel.readString();
hours = parcel.readString();
isOpen = parcel.readString();
content = parcel.readString();
image = parcel.readString();
phone = parcel.readString();
lat = parcel.readDouble();
lng = parcel.readDouble();
}
//creator - used when un-parceling our parcle (creating the object)
public static final Parcelable.Creator<ListingsModel> CREATOR = new Parcelable.Creator<ListingsModel>(){
@Override
public ListingsModel createFromParcel(Parcel parcel) {
return new ListingsModel(parcel);
}
@Override
public ListingsModel[] newArray(int size) {
return new ListingsModel[0];
}
};
//return hashcode of object
public int describeContents() {
return hashCode();
}
}
我需要活动 ReviewActivity 来启动预先填充可打包数据的活动。我无法弄清楚导致我的意图调用的 putExtraData 部分出现错误的问题。
解决方案
当传递模型作为parcelable使用这个:
Bundle b = new Bundle();
b.putParcelable("keyName", YourModel);
startActivity(new Intent(this, Activity.class).putExtra("bundleName", bundle));
将模型作为可包裹接收:
Bundle bundle= getIntent().getBundleExtra("bundleName");
bundle.getParcelable("keyName");
推荐阅读
- google-sheets - 检查名称以在选项卡上导入值
- php - woocommerce 类别和子类别标题挂钩
- aws-lambda - 如何为无服务器 AWS 划分 YML 文件
- php - array_udiff 在带有字符串和对象的数组上
- flutter - 带有侧抽屉导航的 Flutter 底部导航栏
- python - 如何在 ax.set_title 和 fig.savefig 中使用变量
- ios - 如何在不更改时间的情况下快速将字符串转换为日期
- excel - 从公用文件夹中导出 Outlook 电子邮件以放入 Excel 工作表中
- python - 虽然循环没有加起来我的变量 python3
- android - 找不到具有权限 com.scanlibrary.provider 的提供者的元数据