Android Object transfer and parcel mechanism

Source: Internet
Author: User

In the development of many to use the activity directly transfer objects, let's look at, in fact, in Java almost
Custom Object Delivery: There are two ways to pass a custom object through intent
The first is to implement serialization interface;
The second is to implement Parcelable interface;

Let's look at an example:

Package Com.example.bean; import java.io.Serializable; Public classBed implements Serializable {PrivateString name; Private intNumber ;  PublicString GetName () {returnname; }   Public voidsetName (String name) { This. Name =name; }   Public intGetNumber () {returnNumber ; }   Public voidSetnumber (intNumber ) {    This. Number =Number ; } @Override PublicString toString () {return "Name:"+ getName () +", Number:"+GetNumber (); } }
Package Com.example.bean; import android.os.Parcel; import android.os.Parcelable; Public classPatient implements parcelable {PrivateString name;Private intNumber ; PublicString GetName () {returnname;}  Public voidsetName (String name) { This. Name =name;}  Public intGetNumber () {returnNumber ;}  Public voidSetnumber (intNumber ) {   This. Number =Number ;}  Public StaticFinal parcelable.creator<patient> Creator =NewParcelable.creator<patient>() {   PublicPatient createfromparcel (Parcel p) {Patient Patient=NewPatient (); Patient.name=p.readstring (); Patient.number=P.readint (); returnpatient; }   PublicPatient[] NewArray (intsize) {   return NewPatient[size]; } }; @Override Public intdescribecontents () {return 0; } /** * * @desc Note: The Write method here and Createfromparcel in the corresponding one by one, or Createfromparcel are p.readstring (), how do you know which one **/@Override Public voidWritetoparcel (Parcel p,intflags)  {p.writestring (name); P.writeint (number); } @Override PublicString toString () {return "Name:"+ getName () +", Number:"+getnumber ();}}
TestActivity.java:StringBuilder SB=NewStringBuilder (); Sb.append ("Serializable Bed object \ n"); Bed Bed= (Bed) getintent (). Getextras ().Get("Bed");  Sb.append (Bed.tostring ()); Sb.append ("\n\nlist<serializable bed> collection \ n"); List<Bed> listbed = (list<bed>) getintent (). Getserializableextra ("Beds");  for(inti =0; I < listbed.size (); i++) {sb.append (listbed.Get(i). toString () +";"); } sb.append ("\n\nparcelable Patient object \ n"); Patient Patient= Getintent (). Getparcelableextra ("Patient1");  Sb.append (Patient.tostring ()); Sb.append ("\n\nparcelable Patient byte[]\n"); byte[] data = Getintent (). Getbytearrayextra ("Patient2"); if(Data! =NULL) {Parcelinch=Parcel.obtain (); inch. Unmarshall (Data,0, data.length); inch. Setdataposition (0); Patient= Patient.CREATOR.createFromParcel (inch);  } sb.append (Patient.tostring ()); Sb.append ("\n\nstring array \ n"); string[] Str= Getintent (). Getstringarrayextra ("Patients");  for(inti =0; i < str.length; i++) {sb.append (str[i]); } TextView TV=(TextView) Findviewbyid (r.id.tv); Tv.settext (SB);

Android Object transfer and parcel mechanism

Related Article

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.