Applications in the android parcelable Project

Source: Internet
Author: User

Data may be transferred between activities. If it is a basic type and a string type, it is easy to do. Android already provides related APIs.

However, if you want to pass an object class, such as a friend class: buddyinfo, there is no relevant API for calling. You can use parcebale at this time.

ViewCode:

Entity class:

Import android. OS. parcel; import android. OS. parcelable; public class buddyinfo implements parcelable {private string nickname; private string SIG; private int image; Public buddyinfo (string nickname, string Sig, int image) {setnickname (nickname ); setsig (SIG); setimage (image);} public buddyinfo (parcel source) {nickname = source. readstring (); Sig = source. readstring (); image = source. readint () ;}@ overridepublic int describecontents () {return 0;} public static final parcelable. creator <buddyinfo> creator = new creator <buddyinfo> () {@ overridepublic buddyinfo [] newarray (INT size) {return New buddyinfo [size];} @ overridepublic buddyinfo createfromparcel (parcel source) {return New buddyinfo (source) ;};@ overridepublic void writetoparcel (parcel DEST, int flags) {DeST. writestring (nickname); DeST. writestring (SIG); DeST. writeint (image);} Public String getnickname () {return nickname;} public void setnickname (string nickname) {This. nickname = nickname;} Public String getsig () {return SIG;} public void setsig (string sig) {This. sig = SIG;} public int getimage () {return image;} public void setimage (INT image) {This. image = image ;}}

Encapsulate in intent:

 
Mbuddyinfo = newbuddyinfo (*******************); intent = new intent (this, chatactivity. class); intent. putextra (Buddy, mbuddyinfo); startactivity (intent );

Get data:

 
Mintent = This. getintent (); parcelable = mintent. getparcelableextra (Buddy); mbuddyinfo = (buddyinfo) parcelable; mbuddyname = mbuddyinfo. getnickname ();

Is it easy?

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.