Android parcelable serialization of complex data structures

Source: Internet
Author: User

Reference Blog http://blog.csdn.net/yangzl2008/article/details/7593226

Due to the project needs, the activity is to pass a particularly complex data structure object, because the previous serialization is directly using serializable, but serializable can not pass the complex data structure, view the method of the bundle, know parcelable

The use of parcelable looks complicated, but it is also quite simple, that is, using the Writetoparcel (Parcel Dest,int Flags) method to write the attributes in your class to the Parcel object, The Createfromparcel (Parcel in) of the creator is parsed when it is received.

The difficulty I encounter is that there are enumerated and multi-level list collections in the data structure:

Since the enum implements the serializable interface, it can be read through in.readserializable (), and the Enum object's. Ordinal () method gets the index of the enumeration value written to the parcel object, in In.readint ( ) and then parse out the enumeration values.

As for the serialization of the list collection It hurts me to say what I'm doing.

1.Parcelable There are two methods that are arraylist<myclass> list = In.readarraylist (ClassLoader); In.readlist (List, ClassLoader) This time involves the ClassLoader parameter settings, view the source code has a lot of ClassLoader choice, I chose a Serializable.class.getClassLoader (), and then the serialization result is a conversion error when the list collection is serialized into a number and then passed to the adapter in the next activity. This is not the way to solve the problem, but I think this method should be able to achieve, but where there are problems, not written right. If the reader has achieved this idea, we can communicate.

There is also a method in 2.Parcelable in.readtypedlist (recodelist,trainrecode.creator); The first parameter is the list collection for the output, and the second parameter is the creator static constant in the list child element class.

As a result of this method, the serialization is passed normally.

3. One way to see it on the StackOverflow is

   List< object> myList =  Span class= "KWD" >null; Parcel. Readlist (mylist,list . class. Getclassloader Category.mylist I'm using the List.class.getClassLoader () class loader, but I haven't tried it and I don't know if it works. 
/span>

Note: One of them is writing writetoparcel (...) Methods and Createfromparcel (...) The parcel object is written and read in the same order as in the method, otherwise there will be an order exception and garbled characters.

Android parcelable serialization of complex data structures

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.