[Android Data Transmission] intent transmits list and object and list (with source code)

Source: Internet
Author: User
 
1. pass list 
Pass the list <string> Method 
ArrayList<String> info = new ArrayList<String>();info.add(name);info.add(website);info.add(weibo);Intent intent = new Intent(MainActivity.this, ResultActivity.class);intent.putStringArrayListExtra("infoList", info);startActivity(intent);
Method for receiving list <string>
 ArrayList<String> infoList = new ArrayList<String>(); infoList = getIntent().getStringArrayListExtra("infoList");
Method for passing list <integer>
intent.putIntegerArrayListExtra(key, list);
Receive List <integer> Method
list =(ArrayList<Integer>) getIntent().getIntegerArrayListExtra(key);
Ii. Passing objects

There are two ways to pass an object: serializable and parcelable.

2.1 use serializablePremise: the object must implement the serializable interface and use the serializable method to pass the object Syntax: bundle. putserializable (Key, object); syntax for receiving objects in serializable mode: Object = (object) getintent (). getserializableextra (key); the serializable interface is to serialize the object and then transmit it. It is no different from common Java programming, and the object does not need to be significantly changed. We recommend this method. Object serializable
Package COM. wirelessqa. testintent; import Java. io. serializable;/*** implement serializable object * @ author bixiaopeng 11:32:19 */public class serinfo implements serializable {private string name; private string website; private string Weibo; Public serinfo () {} public serinfo (string name, string website, string Weibo) {This. name = Name; this. website = website; this. weibo = Weibo;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String getwebsite () {return website;} public void setwebsite (string website) {This. website = website;} Public String getweibo O () {return Weibo;} public void setweibo O (string Weibo) {This. weibo = Weibo ;}}
Use serializable to pass objects
SerInfo serInfo = new SerInfo(name, website, weibo);                Intent intent = new Intent();                Bundle bundle = new Bundle();                bundle.putSerializable("serinfo", serInfo);                intent.setClass(MainActivity.this, ResultActivity.class);                intent.putExtras(bundle);                startActivity(intent);
Use serializable to receive objects
// Obtain the value serinfo = (serinfo) getintent (). getserializableextra ("serinfo") passed in serializable mode ");
2.2 parcelablePremise: the parcelable interface of the object must be implemented to pass the object syntax in parcelable mode: bundle. putparcelable (Key, object); syntax for receiving objects in parcelable mode: Object = (object) getintent (). getparcelableextra (key );
The classes that implement the parcelable interface are complicated. What is parcelable?
Android provides a new type: parcel, which is used as a container to encapsulate data. encapsulated data can be transmitted through intent or IPC. Except for the basic type, only classes that implement the parcelable interface can be put into parcel.
Three methods are required to implement the parcelable interface:1) writetoparcel method. This method writes the data of the class to the external provided parcel. Declaration: writetoparcel (parcel DEST, int flags ). 2) describecontents method. Return 0 directly. 3) Static parcelable. creator <t> interface. This interface has two methods: createfromparcel (parcel in) to create a class instance from in.
Newarray (INT size) creates an array of T type and size, returnnew T [size. This method is used for external class deserialization of this class array. The parcelable interface must be implemented for the object.
Package COM. wirelessqa. testintent; import android. OS. parcel; import android. OS. parcelable;/*** the object must implement the parcelable interface * @ author bixiaopeng 11:32:19 */public class parinfo implements parcelable {private string name; private string website; private string Weibo; public parinfo () {} public parinfo (string name, string website, string Weibo) {This. name = Name; this. website = website; this. wei BO = Weibo;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String getwebsite () {return website;} public void setwebsite (string website) {This. website = website;} Public String getweibo O () {return Weibo;} public void setweibo O (string Weibo) {This. weibo = Weibo ;}@ override public int describecontents () {// todo auto-generated method St UB return 0;} // This method writes the class data to the external provided parcel. @ Override public void writetoparcel (parcel DEST, int flags) {DeST. writestring (name); DeST. writestring (website); DeST. writestring (Weibo);} public static final parcelable. creator <parinfo> creator = new creator <parinfo> () {// implement the function of creating a class instance from the source @ override public parinfo createfromparcel (parcel source) {parinfo = new parinfo (); parinfo. name = source. readstring (); parinfo. website = source. readstring (); parinfo. weibo = source. readstring (); Return parinfo;} // create an array of the T type and size @ override public parinfo [] newarray (INT size) {return New parinfo [size] ;}};}
Pass object in parcelable Mode
Parinfo = new parinfo (name, website, Weibo); intent = new intent (); bundle = new bundle (); bundle. putparcelable ("parinfo", parinfo); // The writetoparcel method is called here to write data intent to DeST. setclass (mainactivity. this, resultactivity. class); intent. putextras (bundle); startactivity (intent );
Receive objects in parcelable Mode
// Obtain the parcelable value parinfo = (parinfo) getintent (). getparcelableextra ("parinfo"); // The usercreatefromparcel method is called and the parinfo instance is returned.
3. Pass the list <Object> method of passing the list <Object>
                ArrayList<SerInfo> listObj = new ArrayList<SerInfo>();                SerInfo serInfo1 = new SerInfo(name, website, weibo);                SerInfo serInfo2 = new SerInfo(name, website, weibo);                listObj.add(serInfo1);                listObj.add(serInfo2);                Intent intent = new Intent();                intent.setClass(MainActivity.this, ResultActivity.class);                intent.putExtra("listobj", (Serializable) listObj);                startActivity(intent);
Method for receiving list <Object>
// Get the passed list <Object> arraylist <serinfo> listobj = (arraylist <serinfo>) getintent (). getserializableextra ("listobj"); source download: http://pan.baidu.com/share/link? Consumer id = 266032 & UK = 436271564

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.