c1scok1d
c1scok1d

Reputation: 57

Transfer Data between Activities with Android Parcelable

I'm trying to trasnfer data between activities with parcelable I'm having a bit of an issue. I'm getting the error Expression expected on the putExtraData portion of the code. The debugger says error: cannot find symbol variable ListingsModel. The class containing the parcelable code is called ListingsModel as is the constructor. I can’t seem to locate my error. My intent is wrapped in an if statement that should execute if try and launch the activity ReviewActivity.class and pass all data accordingly. Any help would be greatly appreciated.

Intent to launch Review activity class and pass data on true if statement:

Intent intent = new Intent(MainActivity.this, ReviewActivity.class);
                            putExtraData("ListingsModel", **ListingsModel**); //error appears here
                            startActivity(intent);

Class containing parcelable code:


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();
    }
}

I need the activity ReviewActivity to lauch pre-poulated with the parcelable data. I can't figure out the issue that's causing the error on the putExtraData portion of my intent call.

Upvotes: 1

Views: 1312

Answers (2)

Sumit Shukla
Sumit Shukla

Reputation: 4504

When passing model as parcelable use this:

Bundle b = new Bundle();
b.putParcelable("keyName", YourModel);
startActivity(new Intent(this, Activity.class).putExtra("bundleName", bundle));

Receive model as parcelable:

Bundle bundle= getIntent().getBundleExtra("bundleName");
bundle.getParcelable("keyName");

Upvotes: 1

Dmitrii Leonov
Dmitrii Leonov

Reputation: 1389

    ListingModel listingModel = new ListingModel(/* put required parameters here*/);
    Intent intent = new Intent(this, ReviewActivity.class);
    intent.putExtra("ListingsModel", listingModel);
    startActivity(intent);

Upvotes: 0

Related Questions