Android transmits ArrayList with a custom class via intent

Source: Internet
Author: User

Preface the previous project by intent just transfer simple strings, this time because you need to get the object data in the previous page network, and then use the next page, so consider the use of intent transport contains custom class ArrayList.
Serializablejava object serialization refers to the conversion of objects that implement the serializable interface into a sequence of bytes and the ability to fully revert the sequence of bytes to the previous object when needed.
To implement the serialization of an object, you need to implement the Java.io.Serializable interface (note that this interface is just a markup interface, and there is no specific way to override it). Of course, you can also serialize the object yourself, but I think since Java provides a mechanism for object serialization, we'd better use an official approach.
Example create a simple object and implement the Serializable interface
Package Javastudy;import Java.io.serializable;public class person implements Serializable {private static final long Seri Alversionuid = -6470574927973900913l;private string Firstname;private string secondname;//example for Transinetprivate Transient string noserializablestring;public person (string firstName, String secondname, String noserializablestring) { Super (); this.firstname = Firstname;this.secondname = Secondname;this.noserializablestring = noSerializableString;} Public String Getfirstname () {return firstName;} public void Setfirstname (String firstName) {this.firstname = FirstName;} Public String Getsecondname () {return secondname;} public void Setsecondname (String secondname) {this.secondname = Secondname;} Public String getnoserializablestring () {if (noserializablestring! = null) {return noserializablestring;} else {return "" ;}} public void setnoserializablestring (String noserializablestring) {this.noserializablestring = noserializablestring;} Public String toString () {return ' person [First Name: "+ getfirstname () +", second name: "+ getsecondname () +", no serializable: "+ getnoserializablestring () + "]";}}

Write a class that implements serialization and deserialization of objects
Package Javastudy;import Java.io.fileinputstream;import Java.io.fileoutputstream;import java.io.IOException;import Java.io.objectinputstream;import Java.io.objectoutputstream;public class Testserializable {public static void main ( String[] (args) {person person = new Person ("Wang", "Zhengyi", "Genius"); String fileName = "/tmp/person.out";//Save object to Filefileoutputstream fos = null;objectoutputstream out = null;try {f OS = new FileOutputStream (fileName), out = new ObjectOutputStream (FOS), out.writeobject (person); Out.flush ();} catch (Exception e) {e.printstacktrace ();} finally {if (FOS! = null) {try {fos.close ();} catch (IOException e) {E.printsta Cktrace ();}} if (out! = null) {try {out.close ();} catch (IOException e) {e.printstacktrace ();}}}  Read object from Filefileinputstream fin = null;objectinputstream in = null;try {fin = new FileInputStream (fileName); = new ObjectInputStream (FIN); Person P = (person) in.readobject (); SYSTEM.OUT.PRINTLN (P);} catch (Exception e) {e.printstacktrace ();} Finally {if (fin! = null) {try {fin.close ()} catch (IOException e) {e.printstacktrace ()}} if (in = null) {try {in.close ()} catch (IOException e) {e.printstacktrace ();}}}}

Intent transport contains a custom class ArrayList the reason why serializable was introduced earlier is that this is the precondition for intent transmission. ArrayList contains custom classes that must implement the serializable interface to be passed through the Putserializable () method.
or using the person class above as a custom class, the first activity key code to pass ArrayList is as follows:
Intent Creation and Initializationintent passintent = new Intent ();p Assintent.setclass (Mainactivity.this, Secondaryactivity.class);                 Create Custom class Objectperson p1 = new Person ("Wang", "Zhengyi", "first"); person P2 = new Person ("Chen", "Shan", "second");                 Create Array List of custom Class and add the Created objectarraylist<person> alistclass = new Arraylist<perso N> (); Alistclass.add (p1); Alistclass.add (p2)                 ; Create a bundle and Put bundle in to itbundle Bundleobject = new Bundle (); Bundleobject.putserializable ("Key", Alistclas s);                 Put Bundle in to Intent and call start Activitypassintent.putextras (bundleobject); startactivity (passintent);

The second activity key code to receive ArrayList is as follows:
try{    //Get The bundle Object            Bundle Bundleobject = Getintent (). Getextras ();                     Get ArrayList Bundle    arraylist<person> classobject = (arraylist<person>) Bundleobject.getserializable ("key");                     Retrieve Objects from Bundle for    (int index = 0, index < classobject.size (); index++) {person person                         = Classobje Ct.get (index);        Toast.maketext (Getapplicationcontext (), person, Toast.length_short). Show ();    }} catch (Exception e) {    e.printstacktrace ();}


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.