热门IT资讯网

Intent传递数据

发表于:2024-11-30 作者:热门IT资讯网编辑
编辑最后更新 2024年11月30日,http://blog.csdn.net/sukyle/article/details/44855051.Activity之间传递数据(1)利用Intent传递数据传递数据的Activity中:Int

http://blog.csdn.net/sukyle/article/details/4485505

1.Activity之间传递数据

(1)利用Intent传递数据

传递数据的Activity中:

Intent intent = new Intent();
intent.putExtra("name","Jon");//在Intent中加入键值对数据。键:name,值:Jon
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);

在取出数据的Activity中:

Intent intent = getIntent();//获得传过来的Intent。
String value = intent.getStringExtra("name");//根据键name取出值。

(2)利用Bundle传递数据

传递数据的Activity:

Intent intent = new Intent();
Bundle myBundle = new Bundle();
myBundle.putString("Key_Name","Tom");
intent.putExtras(myBundle);
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);

取出数据的Activity:

Bundle getBundle = getIntent().getExtras();
String value = getBundle.getString("Key_Name");

(3)利用startActivityForResult传递数据

startActivityForResult可以把数据传过去,还可以把那边的数据传过来。

传递数据的Activity中:

Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("data", "somedata");//把数据传过去
intent.putExtras(bundle);
intent.setClass(Activity01.this, Activity02.class);
startActivityForResult(intent, 10);//10是一个代码

重载onActivityResult方法,用来接收传过来的数据:

protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
switch (resultCode) {
case RESULT_OK:
Bundle b = intent.getExtras();
String str = b.getString("Result");
setTitle("Return data:" + str);
break;
default:
break;
}
}

接收数据的Activity:

Intent intent = getIntent();
Bundle getBundle = getIntent().getExtras();
String data = getBundle.getString("data");//读取传过来的数据
et.setText(data);

EditText edittext = (EditText) findViewById(R.id.text);
Intent intent = new Intent();//实例化一个Intent用来传过去,可以在Intent里存放数据。
Bundle bundle = new Bundle();
bundle.putString("Result",edittext.getText().toString());
intent.putExtras(bundle);
Activity02.this.setResult(RESULT_OK,intent);//把Intent(数据)传过去,RESULT_OK是请求码。
finish();//结束当前的Activity。

2.http://xqjay19910131-yahoo-cn.iteye.com/blog/1280857

Activity传递一个或者多个对象

Activity之间传递对象,或者通过Bundle传递对象的两种方式。

1:Serializable方式
传递一个对象

2:Parcelable方式
传递一个对象、传递多个对象(ArrayList)


方式一:Serializable

传递类:

Java代码
  1. public class CustomeClass implements Serializable{
  2. /**
  3. *
  4. */
  5. private static final long serialVersionUID = -7060210544600464481L;
  6. private String name;
  7. private String id;
  8. private int age;
  9. private String sex;
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public String getId() {
  17. return id;
  18. }
  19. public void setId(String id) {
  20. this.id = id;
  21. }
  22. public int getAge() {
  23. return age;
  24. }
  25. public void setAge(int age) {
  26. this.age = age;
  27. }
  28. public String getSex() {
  29. return sex;
  30. }
  31. public void setSex(String sex) {
  32. this.sex = sex;
  33. }
  34. }
public class CustomeClass implements Serializable{                /**         *          */        private static final long serialVersionUID = -7060210544600464481L;        private String name;        private String id;        private int age;        private String sex;                public String getName() {                return name;        }        public void setName(String name) {                this.name = name;        }        public String getId() {                return id;        }        public void setId(String id) {                this.id = id;        }        public int getAge() {                return age;        }        public void setAge(int age) {                this.age = age;        }        public String getSex() {                return sex;        }        public void setSex(String sex) {                this.sex = sex;        }}




发送部分:

Java代码
  1. CustomeClass cc = new CustomeClass();
  2. cc.setAge(21);
  3. cc.setId("123456");
  4. cc.setName("mingkg21");
  5. cc.setSex("男");
  6. Intent intent = new Intent(this, PersonInfo.class);
  7. intent.putExtra("PERSON_INFO", cc);
  8. startActivity(intent);
        CustomeClass cc = new CustomeClass();        cc.setAge(21);        cc.setId("123456");        cc.setName("mingkg21");        cc.setSex("男");                Intent intent = new Intent(this, PersonInfo.class);        intent.putExtra("PERSON_INFO", cc);        startActivity(intent);




接收部分:

Java代码
  1. Intent intent = getIntent();
  2. CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO");
  3. setTextView(R.id.id, cc.getId());
  4. setTextView(R.id.name, cc.getName());
  5. setTextView(R.id.sex, cc.getSex());
  6. setTextView(R.id.age, String.valueOf(cc.getAge()));
        Intent intent = getIntent();        CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO");        setTextView(R.id.id, cc.getId());        setTextView(R.id.name, cc.getName());        setTextView(R.id.sex, cc.getSex());        setTextView(R.id.age, String.valueOf(cc.getAge()));




方式二:Parcelable

传递类:

Java代码
  1. public class CustomeParcelable implements Parcelable {
  2. private String name;
  3. private String id;
  4. private int age;
  5. private String sex;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public String getId() {
  13. return id;
  14. }
  15. public void setId(String id) {
  16. this.id = id;
  17. }
  18. public int getAge() {
  19. return age;
  20. }
  21. public void setAge(int age) {
  22. this.age = age;
  23. }
  24. public String getSex() {
  25. return sex;
  26. }
  27. public void setSex(String sex) {
  28. this.sex = sex;
  29. }
  30. public static final Parcelable.Creator CREATOR = new Creator(){
  31. public CustomeParcelable createFromParcel(Parcel source) {
  32. // TODO Auto-generated method stub
  33. CustomeParcelable cus = new CustomeParcelable();
  34. cus.name = source.readString();
  35. cus.id = source.readString();
  36. cus.age = source.readInt();
  37. cus.sex = source.readString();
  38. return cus;
  39. }
  40. public CustomeParcelable[] newArray(int size) {
  41. // TODO Auto-generated method stub
  42. return new CustomeParcelable[size];
  43. }
  44. };
  45. public int describeContents() {
  46. // TODO Auto-generated method stub
  47. return 0;
  48. }
  49. public void writeToParcel(Parcel dest, int flags) {
  50. // TODO Auto-generated method stub
  51. dest.writeString(name);
  52. dest.writeString(id);
  53. dest.writeInt(age);
  54. dest.writeString(sex);
  55. }
  56. }
public class CustomeParcelable implements Parcelable {                private String name;        private String id;        private int age;        private String sex;        public String getName() {                return name;        }        public void setName(String name) {                this.name = name;        }        public String getId() {                return id;        }        public void setId(String id) {                this.id = id;        }        public int getAge() {                return age;        }        public void setAge(int age) {                this.age = age;        }        public String getSex() {                return sex;        }        public void setSex(String sex) {                this.sex = sex;        }                public static final Parcelable.Creator CREATOR = new Creator(){                public CustomeParcelable createFromParcel(Parcel source) {                        // TODO Auto-generated method stub                        CustomeParcelable cus = new CustomeParcelable();                        cus.name = source.readString();                        cus.id = source.readString();                        cus.age = source.readInt();                        cus.sex = source.readString();                        return cus;                }                public CustomeParcelable[] newArray(int size) {                        // TODO Auto-generated method stub                        return new CustomeParcelable[size];                }                        };        public int describeContents() {                // TODO Auto-generated method stub                return 0;        }        public void writeToParcel(Parcel dest, int flags) {                // TODO Auto-generated method stub                dest.writeString(name);                dest.writeString(id);                dest.writeInt(age);                dest.writeString(sex);        }}



发送部分:

Java代码
  1. CustomeParcelable cc = new CustomeParcelable();
  2. cc.setAge(21);
  3. cc.setId("123456");
  4. cc.setName("mingkg21");
  5. cc.setSex("男");
  6. Intent intent = new Intent(this, PersonInfo.class);
  7. intent.putExtra("PERSON_INFO", cc);
  8. startActivity(intent);
CustomeParcelable cc = new CustomeParcelable();cc.setAge(21);cc.setId("123456");cc.setName("mingkg21");cc.setSex("男");        Intent intent = new Intent(this, PersonInfo.class);intent.putExtra("PERSON_INFO", cc);startActivity(intent);



接受部分:

Java代码
  1. Intent intent = getIntent();
  2. CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO");
  3. setTextView(R.id.id, cc.getId());
  4. setTextView(R.id.name, cc.getName());
  5. setTextView(R.id.sex, cc.getSex());
  6. setTextView(R.id.age, String.valueOf(cc.getAge()));
Intent intent = getIntent();CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO");setTextView(R.id.id, cc.getId());setTextView(R.id.name, cc.getName());setTextView(R.id.sex, cc.getSex());setTextView(R.id.age, String.valueOf(cc.getAge()));




以上为Parcelable传递一个对象,若要实现传递多个对象,
传递部分:

Java代码
  1. Bundle bundle = new Bundle();
  2. bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs);
  3. msg.setData(bundle);
  4. endDocNotice.sendMessage(msg);
Bundle bundle = new Bundle();                bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs);                msg.setData(bundle);                endDocNotice.sendMessage(msg);



接受部分:

Java代码
  1. Bundle bundle = msg.getData();
  2. mP3TagForNetDTOs = bundle.getParcelableArrayList("mP3TagForNetDTOs");



0