Data transfer between Android's different activity

Source: Internet
Author: User

1. Transfer Value activity

 PackageMydemo.mycom.demo2;Importandroid.content.Intent;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Button;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;Importjava.util.List;ImportMydemo.mycom.demo2.dao.UserDao;ImportMydemo.mycom.demo2.entity.UserInfo; Public classHomeextendsActionbaractivityImplementsView.onclicklistener {PrivateListView LV; PrivateButton Btn_home_to_login; PrivateList<userinfo>list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_home); Btn_home_to_login=(Button) Findviewbyid (R.id.btn_home_to_login); LV=(ListView) Findviewbyid (r.id.lv); Btn_home_to_login.setonclicklistener ( This); Userdao DAO=NewUserdao ( This); List=Dao.findall (); Lv.setadapter (NewMyadapter ()); Lv.setonitemclicklistener (NewMyitemclicklistener ()); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (r.menu.menu_home, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); } @Override Public voidOnClick (view view) {Intent Intent=NewIntent (); Intent.setclass (Home. This, Register.class);    StartActivity (Intent); }     Public classMyadapterextendsBaseadapter { Public intGetCount () {returnlist.size (); } @Override PublicObject GetItem (inti) {return NULL; } @Override Public LongGetitemid (inti) {return0; } @Override PublicView GetView (intI, view view, ViewGroup ViewGroup) {UserInfo User=List.get (i); View Itemview= View.inflate (Home. This, R.layout.activity_item,NULL); TextView tv_id=(TextView) Itemview.findviewbyid (r.id.tv_id); TextView Tv_username=(TextView) Itemview.findviewbyid (r.id.tv_username); TextView Tv_password=(TextView) Itemview.findviewbyid (R.id.tv_password); Tv_id.settext (User.getid ()+"");            Tv_username.settext (User.getusername ());            Tv_password.settext (User.getpassword ()); returnItemview; }    }     Public classMyitemclicklistenerImplementsAdapterview.onitemclicklistener {@Override Public voidOnitemclick (adapterview<?> adapterview, view view,intILongl) {ListView LView=(ListView) LV; TextView tv_id=(TextView) View.findviewbyid (r.id.tv_id); TextView Tv_username=(TextView) View.findviewbyid (r.id.tv_username); TextView Tv_password=(TextView) View.findviewbyid (R.id.tv_password); String ID=Tv_id.gettext (). toString (); String username=Tv_username.gettext (). toString (); String Password=Tv_password.gettext (). toString (); Toast.maketext (Getapplicationcontext (), id+","+username, toast.length_short). Show (); //the value of the transfer between different activityIntent Intent =NewIntent (); Intent.putextra ("Username", username); Intent.putextra ("Password", password); Intent.putextra ("id", id); Intent.setclass (Home. This, Edit.class);        StartActivity (Intent); }    }}

2. Accept Activity

 PackageMydemo.mycom.demo2;Importandroid.content.Intent;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;Importandroid.text.TextUtils;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportOrg.w3c.dom.Text;ImportMydemo.mycom.demo2.dao.UserDao; Public classEditextendsActionbaractivityImplementsView.onclicklistener {PrivateEditText Edit_et_username; PrivateEditText Edit_et_password; PrivateTextView edit_tv_id; PrivateButton Btn_edit; PrivateButton Btn_home; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_edit); Edit_et_username=(EditText) Findviewbyid (r.id.edit_et_username); Edit_et_password=(EditText) Findviewbyid (R.id.edit_et_password); edit_tv_id=(TextView) Findviewbyid (r.id.edit_tv_id); Btn_edit=(Button) Findviewbyid (R.id.btn_edit); Btn_home=(Button) Findviewbyid (r.id.btn_home); Btn_edit.setonclicklistener ( This); Btn_home.setonclicklistener ( This); //accept the value that the activity passes overIntent Intent =getintent (); String username= Intent.getstringextra ("username"); String Password= Intent.getstringextra ("Password"); String ID= Intent.getstringextra ("id");        Edit_et_username.settext (username);        Edit_et_password.settext (password);    Edit_tv_id.settext (ID); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.menu_edit, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); } @Override Public voidOnClick (view view) {Switch(View.getid ()) { Caser.id.btn_edit:string ID=Edit_tv_id.gettext (). toString (); String Password=Edit_et_password.gettext (). toString (). Trim (); String username=Edit_et_username.gettext (). toString (). Trim (); if(textutils.isempty (password) | |Textutils.isempty (username)) {Toast.maketext ( This, "The user name or password cannot be empty", Toast.length_short). Show (); return; } Userdao DAO=NewUserdao ( This); Booleanresult =Dao.findbyusername (username); if(Result) {Toast.maketext ( This, "User has been registered", Toast.length_short). Show (); return;                } dao.modify (Id,username,password); Toast.maketext ( This, "Modified successfully", Toast.length_short). Show (); Intent Intent=NewIntent (); Intent.setclass ( This, Home.class);                StartActivity (Intent);  Break;  Caser.id.btn_home:intent tointent=NewIntent (); Tointent.setclass ( This, Home.class);                StartActivity (tointent);  Break; }    }}

Data transfer between Android's different activity

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.