首页 > 解决方案 > 使用 Android Parcelable 在 Activity 之间传输数据

问题描述

我正在尝试使用 parcelable 在活动之间传输数据我遇到了一些问题。我收到代码部分的错误Expression expectedputExtraData调试器说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 部分出现错误的问题。

标签: androidparcelable

解决方案


当传递模型作为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");

推荐阅读