在网上看到一个介绍利用Intent在activity之间传递复杂数据结构对象(如ArrayList)的例子,经过实测可行,记录下来,供以后参考。
感谢
wdaming1986整理,本文转自
http://blog.csdn.net/wdaming1986/article/details/6762633 程序主界面 点击list按钮传递数据
点击parcelable传递数据 点击serializable传递数据
MainAcitivty.java的代码
1import java.io.Serializable;
2import java.util.ArrayList;
3import java.util.HashMap;
4import java.util.List;
5import java.util.Map;
6
7import android.app.Activity;
8import android.app.ListActivity;
9import android.content.Intent;
10import android.graphics.Color;
11import android.graphics.drawable.GradientDrawable;
12import android.graphics.drawable.GradientDrawable.Orientation;
13import android.os.Bundle;
14import android.view.View;
15import android.view.View.OnClickListener;
16import android.widget.Button;
17
18public class MainActivity extends Activity implements Serializable{
19
20 private static final long serialVersionUID = 1L;
21
22 private String s_name;
23 private int s_number;
24 private String s_sex;
25
26 private Button list_Button;
27 private Button ser_Button;
28 private Button par_Button;
29 private ArrayList<String> m_list;
30
31 public final static String PAR_KEY = "com.cn.daming.parcelable";
32 public final static String SER_KEY = "com.cn.daming.serializable";
33 public final static String LIST_KEY = "com.cn.daming.ArrayList";
34 @Override
35 public void onCreate(Bundle savedInstanceState) {
36 super.onCreate(savedInstanceState);
37 setContentView(R.layout.main);
38 initlist();
39 drawBackground();
40 initList_Button();
41 initPar_Button();
42 inintSer_Button();
43 }
44
45 public void initlist()
46 {
47 m_list = new ArrayList<String>();
48 m_list.add("大明ArrayList");
49 m_list.add("年龄:25岁");
50 m_list.add("性别:男");
51 }
52
53 public void drawBackground()
54 {
55 GradientDrawable grad = new GradientDrawable(
56 Orientation.TL_BR,
57 new int[] {Color.rgb(0, 0, 127),
58 Color.rgb(0, 0, 255),
59 Color.rgb(127, 0, 255),
60 Color.rgb(127, 127, 255),
61 Color.rgb(127, 255, 255),
62 Color.rgb(255, 255, 255)}
63 );
64
65 this.getWindow().setBackgroundDrawable(grad);
66 }
67
68 public void initList_Button()
69 {
70 list_Button = (Button)findViewById(R.id.list_button);
71 list_Button.setOnClickListener(new OnClickListener(){
72
73 @Override
74 public void onClick(View arg0) {
75 Intent list_intent = new Intent();
76 list_intent.putStringArrayListExtra(LIST_KEY, m_list);
77 list_intent.setClass(MainActivity.this, ShowListView.class);
78 startActivity(list_intent);
79 }
80 });
81 }
82
83 public void initPar_Button()
84 {
85 par_Button = (Button)findViewById(R.id.par_button);
86 par_Button.setOnClickListener(new OnClickListener(){
87
88 @Override
89 public void onClick(View arg0) {
90 Student m_Student = new Student();
91 m_Student.setName("大明例子");
92 m_Student.setAge(25);
93 m_Student.setSex("男");
94 Intent p_Intent = new Intent(MainActivity.this,ShowParView.class);
95 Bundle mBundle = new Bundle();
96 mBundle.putParcelable(PAR_KEY, m_Student);
97 p_Intent.putExtras(mBundle);
98 startActivity(p_Intent);
99 }
100 });
101 }
102
103 public void inintSer_Button()
104 {
105 ser_Button = (Button)findViewById(R.id.ser_button);
106 ser_Button.setOnClickListener(new OnClickListener(){
107
108 @Override
109 public void onClick(View arg0) {
110 MainActivity s_activity = new MainActivity();
111 s_activity.setS_name("Daming Serlizable!");
112 s_activity.setS_number(25);
113 s_activity.setS_sex("男");
114 Intent mIntent = new Intent(MainActivity.this,ShowSerView.class);
115 Bundle mBundle = new Bundle();
116 mBundle.putInt("state", 3);
117 mBundle.putSerializable(SER_KEY, s_activity);
118 mIntent.putExtras(mBundle);
119 startActivity(mIntent);
120 }
121 });
122 }
123
124
125 public void setS_name(String s_name) {
126 this.s_name = s_name;
127 }
128
129 public String getS_name() {
130 return s_name;
131 }
132
133 public void setS_number(int s_number) {
134 this.s_number = s_number;
135 }
136
137 public int getS_number() {
138 return s_number;
139 }
140
141 public void setS_sex(String s_sex) {
142 this.s_sex = s_sex;
143 }
144
145 public String getS_sex() {
146 return s_sex;
147 }
148}
Student.java代码
1import android.os.Parcel;
2import android.os.Parcelable;
3
4public class Student implements Parcelable{
5
6 private String name;
7 private int age;
8 private String sex;
9
10 public String getName() {
11 return name;
12 }
13 public void setName(String name) {
14 this.name = name;
15 }
16 public int getAge() {
17 return age;
18 }
19 public void setAge(int age) {
20 this.age = age;
21 }
22 public String getSex() {
23 return sex;
24 }
25 public void setSex(String sex) {
26 this.sex = sex;
27 }
28
29 public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {
30 public Student createFromParcel(Parcel source) {
31 Student mStudent = new Student();
32 mStudent.name = source.readString();
33 mStudent.age = source.readInt();
34 mStudent.sex = source.readString();
35 return mStudent;
36 }
37 public Student[] newArray(int size) {
38 return new Student[size];
39 }
40 };
41
42
43 @Override
44 public int describeContents() {
45 // TODO Auto-generated method stub
46 return 0;
47 }
48 @Override
49 public void writeToParcel(Parcel parcel, int arg1) {
50 parcel.writeString(name);
51 parcel.writeInt(age);
52 parcel.writeString(sex);
53 }
54}
55
ShowListView.java代码
1import java.util.ArrayList;
2
3import android.app.Activity;
4import android.content.Intent;
5import android.graphics.Color;
6import android.graphics.drawable.GradientDrawable;
7import android.graphics.drawable.GradientDrawable.Orientation;
8import android.os.Bundle;
9import android.widget.TextView;
10
11public class ShowListView extends Activity{
12
13 private Intent list_intent;
14 private ArrayList<String> m_arrayList;
15 private TextView list_textview;
16
17 @Override
18 protected void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.show_list_view);
21 drawBackground();
22 list_textview = (TextView)findViewById(R.id.list_text_view);
23 list_intent = getIntent();
24 m_arrayList = list_intent.getExtras().getStringArrayList(MainActivity.LIST_KEY);
25 m_arrayList.get(0);
26 list_textview.setText(m_arrayList.get(0)+" \n"+m_arrayList.get(1)+"\n"+m_arrayList.get(2));
27 }
28
29 public void drawBackground()
30 {
31 GradientDrawable grad = new GradientDrawable(
32 Orientation.TL_BR,
33 new int[] {Color.rgb(0, 0, 127),
34 Color.rgb(0, 0, 255),
35 Color.r
1import android.app.Activity;
2import android.graphics.Color;
3import android.graphics.drawable.GradientDrawable;
4import android.graphics.drawable.GradientDrawable.Orientation;
5import android.os.Bundle;
6import android.widget.TextView;
7
8public class ShowParView extends Activity{
9
10 private TextView par_text_view;
11
12 @Override
13 protected void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.show_par_view);
16 drawBackground();
17
18 par_text_view = (TextView)findViewById(R.id.par_text_view);
19 Student p_student = (Student)getIntent().getParcelableExtra(MainActivity.PAR_KEY);
20 par_text_view.setText("姓名: " + p_student.getName()+"\n"+
21 "年龄: " + p_student.getAge() + "\n" +
22 "性别 : " + p_student.getSex() + "\n" +
23 "类:" + p_student.getClass());
24
25 }
26
27 public void drawBackground()
28 {
29 GradientDrawable grad = new GradientDrawable(
30 Orientation.TL_BR,
31 new int[] {Color.rgb(0, 0, 127),
32 Color.rgb(0, 0, 255),
33 Color.rgb(127, 0, 255),
34 Color.rgb(127, 127, 255),
35 Color.rgb(127, 255, 255),
36 Color.rgb(255, 255, 255)}
37 );
38
39 this.getWindow().setBackgroundDrawable(grad);
40 }
41}
42 gb(127, 0, 255),
36 Color.rgb(127, 127, 255),
37 Color.rgb(127, 255, 255),
38 Color.rgb(255, 255, 255)}
39 );
40
41 this.getWindow().setBackgroundDrawable(grad);
42 }
43}
44
ShowParView.java代码