Android: Transmission of complex data models

Source: Internet
Author: User

 

I haven't written it for a long time and have never known what to write. I recently studied how to transfer some complex data in Android development. In fact, many people have already written related content on the Internet. However, after reading the content, I found that the content is similar and missing what I want to see. So I plan to discuss this.

Sometimes, when developing a team, we will consider creating complex (including multiple data types) models to facilitate data transmission and processing. For some android models, Parcelable or Serializable interfaces are generally implemented. These two interfaces are somewhat different. The Serializable interface generally only performs some simple data type transfer, parcelable can implement many complex situations, such as embedding images in Parcelable and implementing other Parcelable models. However, it is difficult to find such content on the Internet for such complex types, so I made an example to solve it.

 

First, I created three data models: ArtistMode, TrackModel, and AlbumModel. You should be able to understand them by name. Among the three classes, only ArtistModel is the implemented Serializable interface, and the other two are implemented Parcelable interfaces.

ArtistModel: information about a singer, including the name, age, gender, and nationality of the singer. These are simple String and int data;

TrackMode: The Song information, including the song name, release time, and singer List (here a List is used to store ArtistMode );

AlbumModel: the album information, including the album name, artist information (ArtistMode), release time, and song List (a List containing TrackModel ).

 

After introducing the three models, we need to read and write data in the model. For Parcelable or Serializable, I will not mention the methods for this rewrite. There are many introductions on the Internet. Now let's talk about something to be aware.

 

 

Java code

@ Override

Public void writeToParcel (Parcel dest, int flag ){

Dest. writeString (albumName );

Dest. writeSerializable (artistModel );

Dest. writeList (trackList );

Dest. writeString (releaseTime );

}

 

Public static final Parcelable. Creator <AlbumModel> CREATOR = new Parcelable. Creator <AlbumModel> (){

 

@ Override

Public AlbumModel createFromParcel (Parcel source ){

 

AlbumModel model = new AlbumModel ();

Model. albumName = source. readString ();

Model. artistModel = (ArtistModel) source. readSerializable ();

// It must be instantiated.

Model. trackList = new ArrayList <TrackModel> ();

Source. readList (model. trackList, getClass (). getClassLoader ());

 

Model. releaseTime = source. readString ();

Return model;

}

 

@ Override

Public AlbumModel [] newArray (int size ){

Return new AlbumModel [size];

}

};

Note that in the writeToParcel () method, I used dest. writeList (trackList); to pass a List. The corresponding read contains the following code:

 

Java code

// It must be instantiated.

Model. trackList = new ArrayList <TrackModel> ();

Source. readList (model. trackList, getClass (). getClassLoader ());

 

To read the stored List, you must first instantiate the List. Otherwise, you cannot read the data. This is my hair style after viewing the Android source code. For Data Reading and writing, the system provides a more comprehensive read method: readValue. In this method, the system will automatically perform data matching, but for specific use, you can try it. Once you understand the read/write method, it is not hard to achieve it.

 

The above is the only thing that requires special attention. You can try to transfer other data types. Next I will upload the entire project. If you have any questions, please leave a message.

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.