The Android programming implementation uses intent to transfer an ArrayList sample containing custom classes

This article describes an example of Android programming using intent to transfer ArrayList containing custom classes. Share with you for your reference, as follows:

preface

In the previous project, only simple strings were transmitted through intent. This time, because the object data needs to be obtained online on the previous page and then used on the next page, it is considered to use intent to transmit ArrayList containing custom classes.

Serializable

Java object serialization refers to converting those objects that implement the serializable interface into a byte sequence, and can completely restore the byte sequence to the previous object when necessary.

To serialize objects, you need to implement the java.io.serializable interface (note that this interface is only a tag interface, and there is no specific method to override). Of course, you can also implement object serialization by yourself, but I think since Java provides such a mechanism for object serialization, we'd better use the officially provided method.

Example

Create a simple object and implement the serializable interface

Write another class to serialize and deserialize the object

The intent transport contains an ArrayList of custom classes

The reason why serializable was introduced earlier is that this is the premise of realizing intent transmission. The user-defined classes contained in ArrayList must implement the serializable interface before they can be passed through the putserializable () method.

If the above person class is still used as a custom class, the key code of the activity first passed to ArrayList is as follows:

The key code of the second activity receiving ArrayList is as follows:

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>