(Android Review) Data transfer between the activity

Source: Internet
Author: User


I. Basic points of knowledge

1. Transfer data between activity
1) Pass basic type or string
Intent.putextra ("username", username);

Getintent ();
Intent.getstringextra ("username");


2) transmission in the form of bundles

Bundle bundle = new bundle ();
Bundle.putstring ("username", username);
Bundle.putstring ("password", password);

Intent.putextras (bundle);




Bundle bundle = Intent.getextras ();
String username = bundle.getstring ("username");
String Password = bundle.getstring ("password");


3) Passing Custom types


1)) Implements Parcelable
2)) Rewrite the following two methods
public int describecontents () {
TODO auto-generated Method Stub
return 0;
}

Write data to the parcel object
public void Writetoparcel (Parcel dest, int flags) {
TODO auto-generated Method Stub
Dest.writeint (ID);
Dest.writestring (name);
Dest.writeint (age);
}


3) Copy the following method (change person to your actual type)


public static final Parcelable.creator<person> Creator
= new Parcelable.creator<person> () {
Reading data from the parcel object
Public person Createfromparcel (Parcel in) {
return new person (in);
}

Public person[] NewArray (int size) {
return new Person[size];
}
};

Public person (Parcel in) {
TODO auto-generated Constructor stub
id = in.readint ();
Name = In.readstring ();
Age = In.readint ();
}




person person = new person (100, "Jeff Chang", 40);
Intent.putextra ("person", person);




Person person = Intent.getparcelableextra ("person");


Second, the Code

1, Mainactivity

Package Com.example.parsedata;import Android.os.bundle;import Android.app.activity;import android.content.Intent; Import Android.view.menu;import Android.view.view;import Android.widget.edittext;public class MainActivity extends Activity {private EditText et_username;private EditText et_password; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); et_username = (EditText) Findviewbyid (r.id.et_username); Et_password = (EditText) Findviewbyid (R.id.et_password);} public void Enter (view view) {String username = Et_username.gettext (). toString (); String password = Et_password.gettext (). toString (); Intent Intent = new Intent (this, mainactivity2.class); Intent.putextra ("username", username); Intent.putextra ("Password", password); Bundle bundle = new bundle (); Bundle.putstring ("Zhangzetian", "Huangjundong"); Intent.putextras (bundle); person person = new person (2, "Hjd is a handsomeboy.."), Intent.putextra (' person ', person); startAcTivity (intent);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


2, MainActivity2

Package Com.example.parsedata;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.view;import Android.widget.edittext;import Android.widget.textview;public class MainActivity2 Extends Activity {private TextView tv_info; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main2); tv_info = (TextView) Findviewbyid (r.id.tv_info); Intent Intent = Getintent (); String username = Intent.getstringextra ("username"); String Password = Intent.getstringextra ("password"); Bundle bundle = Intent.getextras (); Person p = Intent.getparcelableextra ("person"), Tv_info.settext ("Username:" + username + ", password:" + password + "Zhang Zetian ' s husband is: "+ bundle.getstring (" Zhangzetian ") +" Person: "+ p.tostring ());}}


Third, the source code download:

http://download.csdn.net/detail/caihongshijie6/7792903


Four




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.