Transferring objects between Android pages

Source: Internet
Author: User

There are two ways to pass an object on Android:

One is serializable and parcelable.

For the first way:

Import java.io.Serializable;


public class Shopdata implements Serializable {
Public String Shopid;
public String name;
public String URL;
public String info;


Public String Getshopid () {
return shopid;
}


public void Setshopid (String shopid) {
This.shopid = Shopid;
}


Public String GetName () {
return name;
}


public void SetName (String name) {
THIS.name = name;
}


Public String GetUrl () {
return URL;
}


public void SetUrl (String URL) {
This.url = URL;
}


Public String GetInfo () {
return info;
}


public void SetInfo (String info) {
This.info = info;
}


}

This is passed when the value is passed:

<span style= "White-space:pre" ></span>intent Intent = new Intent (); Shopdata Shopdata = (shopdata) madapter.getitem (position);//intent.putextra ("detail", shopdata); Bundle bundle = new Bundle (), bundle.putserializable ("Shop", Shopdata), Intent.putextra ("detail", shopdata); Intent.setclass (Mainactivity.this, Detailshopactivity.class); MainActivity.this.startActivity (Intent);
When you take a value:

<span style= "White-space:pre" ></span>intent Intent = Getintent ();//shopdata data = Intent.getparcelableextra ("detail"); Shopdata data = (shopdata) Intent.getserializableextra ("detail");

For the second type of method:

Import Android.os.parcel;import Android.os.parcelable;public class Shopdata implements Parcelable{public String ShopId ;p ublic String Name;public string Url;public string Info;public shopdata (Parcel in) {Readfromparcel (in);} Public Shopdata () {}public static final parcelable.creator<shopdata> Creator = new Parcelable.creator<shopdata > () {public shopdata createfromparcel (Parcel.) {return new Shopdata (in);} Public shopdata[] NewArray (int size) {return new shopdata[size];}; @Overridepublic int describecontents () {return 0;} @Overridepublic void Writetoparcel (Parcel dest, int flags) {dest.writestring (shopid);d est.writestring (name); Dest.writestring (URL);d est.writestring (info);} public void Readfromparcel (Parcel in) {shopid = in.readstring (); name = In.readstring (); url = in.readstring (); info = in.re Adstring ();}}

This is written when passed:

<span style= "White-space:pre" ></span>intent Intent = new Intent (); Shopdata Shopdata = (shopdata) madapter.getitem (position); Intent.putextra ("detail", shopdata); Intent.putextra (" Detail ", shopdata); Intent.setclass (Mainactivity.this, Detailshopactivity.class); MainActivity.this.startActivity (Intent);


When you take a value:

<span style= "White-space:pre" ></span>intent Intent = Getintent (); Shopdata data = Intent.getparcelableextra ("detail");



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.