使用Android Parcelable在活动之间传输数据

问题描述 投票:0回答:2

我正在尝试使用可拆分的活动在活动之间转移数据,我遇到了一个问题。我在代码的Expression expected部分收到错误putExtraData。调试器显示error: cannot find symbol variable ListingsModel。包含可拆分代码的类与构造函数一样称为ListingsModel。我似乎找不到我的错误。我的意图包含在if语句中,如果尝试并启动活动ReviewActivity.class并相应地传递所有数据,则该if语句应执行。任何帮助将不胜感激。

打算启动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部分出现错误的问题。

android parcelable
2个回答
1
投票

当将模型作为包裹传递时,请使用此:

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");

0
投票
    ListingModel listingModel = new ListingModel(/* put required parameters here*/);
    Intent intent = new Intent(this, ReviewActivity.class);
    intent.putExtra("ListingsModel", listingModel);
    startActivity(intent);
© www.soinside.com 2019 - 2024. All rights reserved.