Android Intent传递复杂类型,例如:数组,ArrayList类型,传递类对象

程序主界面:

点击查看原图

点击list按钮传递数据:

点击查看原图

点击parcelable传递数据:

点击查看原图

点击serializable传递数据:

点击查看原图

下面看代码:

一、MainAcitivty.java类的代码:

001package com.cn.daming;
002 
003
004 
005import java.io.Serializable;
006 
007import java.util.ArrayList;
008 
009import java.util.HashMap;
010 
011import java.util.List;
012 
013import java.util.Map;
014 
015
016 
017import android.app.Activity;
018 
019import android.app.ListActivity;
020 
021import android.content.Intent;
022 
023import android.graphics.Color;
024 
025import android.graphics.drawable.GradientDrawable;
026 
027import android.graphics.drawable.GradientDrawable.Orientation;
028 
029import android.os.Bundle;
030 
031import android.view.View;
032 
033import android.view.View.OnClickListener;
034 
035import android.widget.Button;
036 
037
038 
039public class MainActivity extendsActivity implementsSerializable {
040 
041
042 
043    privatestatic final long serialVersionUID = 1L;
044 
045
046 
047    privateString s_name;
048 
049    privateint s_number;
050 
051    privateString s_sex;
052 
053
054 
055    privateButton list_Button;
056 
057    privateButton ser_Button;
058 
059    privateButton par_Button;
060 
061    privateArrayList<string> m_list;
062 
063
064 
065    publicfinal static String PAR_KEY = "com.cn.daming.parcelable";
066 
067    publicfinal static String SER_KEY = "com.cn.daming.serializable";
068 
069    publicfinal static String LIST_KEY = "com.cn.daming.ArrayList";
070 
071
072 
073    @Override
074 
075    publicvoid onCreate(Bundle savedInstanceState) {
076 
077        super.onCreate(savedInstanceState);
078 
079        setContentView(R.layout.main);
080 
081        initlist();
082 
083        drawBackground();
084 
085        initList_Button();
086 
087        initPar_Button();
088 
089        inintSer_Button();
090 
091    }
092 
093
094 
095    publicvoid initlist() {
096 
097        m_list =new ArrayList<string>();
098 
099        m_list.add("大明ArrayList");
100 
101        m_list.add("年龄:25岁");
102 
103        m_list.add("性别:男");
104 
105    }
106 
107
108 
109    publicvoid drawBackground() {
110 
111        GradientDrawable grad =new GradientDrawable(Orientation.TL_BR,
112 
113                newint[] { Color.rgb(0,0, 127), Color.rgb(0,0, 255),
114 
115                        Color.rgb(127,0, 255), Color.rgb(127,127, 255),
116 
117                        Color.rgb(127,255, 255), Color.rgb(255,255, 255) });
118 
119
120 
121        this.getWindow().setBackgroundDrawable(grad);
122 
123    }
124 
125
126 
127    publicvoid initList_Button() {
128 
129        list_Button = (Button) findViewById(R.id.list_button);
130 
131        list_Button.setOnClickListener(newOnClickListener() {
132 
133
134 
135            @Override
136 
137            publicvoid onClick(View arg0) {
138 
139                Intent list_intent =new Intent();
140 
141                list_intent.putStringArrayListExtra(LIST_KEY, m_list);
142 
143                list_intent.setClass(MainActivity.this, ShowListView.class);
144 
145                startActivity(list_intent);
146 
147            }
148 
149        });
150 
151    }
152 
153
154 
155    publicvoid initPar_Button() {
156 
157        par_Button = (Button) findViewById(R.id.par_button);
158 
159        par_Button.setOnClickListener(newOnClickListener() {
160 
161
162 
163            @Override
164 
165            publicvoid onClick(View arg0) {
166 
167                Student m_Student =new Student();
168 
169                m_Student.setName("大明例子");
170 
171                m_Student.setAge(25);
172 
173                m_Student.setSex("男");
174 
175                Intent p_Intent =new Intent(MainActivity.this,
176 
177                        ShowParView.class);
178 
179                Bundle mBundle =new Bundle();
180 
181                mBundle.putParcelable(PAR_KEY, m_Student);
182 
183                p_Intent.putExtras(mBundle);
184 
185                startActivity(p_Intent);
186 
187            }
188 
189        });
190 
191    }
192 
193
194 
195    publicvoid inintSer_Button() {
196 
197        ser_Button = (Button) findViewById(R.id.ser_button);
198 
199        ser_Button.setOnClickListener(newOnClickListener() {
200 
201
202 
203            @Override
204 
205            publicvoid onClick(View arg0) {
206 
207                MainActivity s_activity =new MainActivity();
208 
209                s_activity.setS_name("Daming Serlizable!");
210 
211                s_activity.setS_number(25);
212 
213                s_activity.setS_sex("男");
214 
215                Intent mIntent =new Intent(MainActivity.this,
216 
217                        ShowSerView.class);
218 
219                Bundle mBundle =new Bundle();
220 
221                mBundle.putInt("state",3);
222 
223                mBundle.putSerializable(SER_KEY, s_activity);
224 
225                mIntent.putExtras(mBundle);
226 
227                startActivity(mIntent);
228 
229            }
230 
231        });
232 
233    }
234 
235
236 
237    publicvoid setS_name(String s_name) {
238 
239        this.s_name = s_name;
240 
241    }
242 
243
244 
245    publicString getS_name() {
246 
247        returns_name;
248 
249    }
250 
251
252 
253    publicvoid setS_number(ints_number) {
254 
255        this.s_number = s_number;
256 
257    }
258 
259
260 
261    publicint getS_number() {
262 
263        returns_number;
264 
265    }
266 
267
268 
269    publicvoid setS_sex(String s_sex) {
270 
271        this.s_sex = s_sex;
272 
273    }
274 
275
276 
277    publicString getS_sex() {
278 
279        returns_sex;
280 
281    }
282 
283}

二、Student.java类的代码:

001package com.cn.daming;
002 
003
004 
005import android.os.Parcel;
006 
007import android.os.Parcelable;
008 
009
010 
011public class Student implementsParcelable {
012 
013
014 
015    privateString name;
016 
017    privateint age;
018 
019    privateString sex;
020 
021
022 
023    publicString getName() {
024 
025        returnname;
026 
027    }
028 
029
030 
031    publicvoid setName(String name) {
032 
033        this.name = name;
034 
035    }
036 
037
038 
039    publicint getAge() {
040 
041        returnage;
042 
043    }
044 
045
046 
047    publicvoid setAge(intage) {
048 
049        this.age = age;
050 
051    }
052 
053
054 
055    publicString getSex() {
056 
057        returnsex;
058 
059    }
060 
061
062 
063    publicvoid setSex(String sex) {
064 
065        this.sex = sex;
066 
067    }
068 
069
070 
071    publicstatic final Parcelable.Creator<student> CREATOR = newCreator<student>() {
072 
073        publicStudent createFromParcel(Parcel source) {
074 
075            Student mStudent =new Student();
076 
077            mStudent.name = source.readString();
078 
079            mStudent.age = source.readInt();
080 
081            mStudent.sex = source.readString();
082 
083            returnmStudent;
084 
085        }
086 
087
088 
089        publicStudent[] newArray(intsize) {
090 
091            returnnew Student[size];
092 
093        }
094 
095    };
096 
097
098 
099    @Override
100 
101    publicint describeContents() {
102 
103        // TODO Auto-generated method stub
104 
105        return0;
106 
107    }
108 
109
110 
111    @Override
112 
113    publicvoid writeToParcel(Parcel parcel,int arg1) {
114 
115        parcel.writeString(name);
116 
117        parcel.writeInt(age);
118 
119        parcel.writeString(sex);
120 
121    }
122 
123}

三、ShowListView.java类的代码:

01package com.cn.daming;
02 
03
04 
05import java.util.ArrayList;
06 
07
08 
09import android.app.Activity;
10 
11import android.content.Intent;
12 
13import android.graphics.Color;
14 
15import android.graphics.drawable.GradientDrawable;
16 
17import android.graphics.drawable.GradientDrawable.Orientation;
18 
19import android.os.Bundle;
20 
21import android.widget.TextView;
22 
23
24 
25public class ShowListView extendsActivity {
26 
27
28 
29    privateIntent list_intent;
30 
31    privateArrayList<string> m_arrayList;
32 
33    privateTextView list_textview;
34 
35
36 
37    @Override
38 
39    protectedvoid onCreate(Bundle savedInstanceState) {
40 
41        super.onCreate(savedInstanceState);
42 
43        setContentView(R.layout.show_list_view);
44 
45        drawBackground();
46 
47        list_textview = (TextView) findViewById(R.id.list_text_view);
48 
49        list_intent = getIntent();
50 
51        m_arrayList = list_intent.getExtras().getStringArrayList(
52 
53                MainActivity.LIST_KEY);
54 
55        m_arrayList.get(0);
56 
57        list_textview.setText(m_arrayList.get(0) +" \n" + m_arrayList.get(1)
58 
59                +"\n" + m_arrayList.get(2));
60 
61    }
62 
63
64 
65    publicvoid drawBackground() {
66 
67        GradientDrawable grad =new GradientDrawable(Orientation.TL_BR,
68 
69                newint[] { Color.rgb(0,0, 127), Color.rgb(0,0, 255),
70 
71                        Color.rgb(127,0, 255), Color.rgb(127,127, 255),
72 
73                        Color.rgb(127,255, 255), Color.rgb(255,255, 255) });
74 
75
76 
77        this.getWindow().setBackgroundDrawable(grad);
78 
79    }
80 
81}

四、ShowParView.java类的代码:

01package com.cn.daming;
02 
03
04 
05import android.app.Activity;
06 
07import android.graphics.Color;
08 
09import android.graphics.drawable.GradientDrawable;
10 
11import android.graphics.drawable.GradientDrawable.Orientation;
12 
13import android.os.Bundle;
14 
15import android.widget.TextView;
16 
17
18 
19public class ShowParView extendsActivity {
20 
21
22 
23    privateTextView par_text_view;
24 
25
26 
27    @Override
28 
29    protectedvoid onCreate(Bundle savedInstanceState) {
30 
31        super.onCreate(savedInstanceState);
32 
33        setContentView(R.layout.show_par_view);
34 
35        drawBackground();
36 
37
38 
39        par_text_view = (TextView) findViewById(R.id.par_text_view);
40 
41        Student p_student = (Student) getIntent().getParcelableExtra(
42 
43                MainActivity.PAR_KEY);
44 
45        par_text_view.setText("姓名: "+ p_student.getName() + "\n"+ "年龄: "
46 
47                + p_student.getAge() +"\n" + "性别 : "+ p_student.getSex()
48 
49                +"\n" + "类:"+ p_student.getClass());
50 
51
52 
53    }
54 
55
56 
57    publicvoid drawBackground() {
58 
59        GradientDrawable grad =new GradientDrawable(Orientation.TL_BR,
60 
61                newint[] { Color.rgb(0,0, 127), Color.rgb(0,0, 255),
62 
63                        Color.rgb(127,0, 255), Color.rgb(127,127, 255),
64 
65                        Color.rgb(127,255, 255), Color.rgb(255,255, 255) });
66 
67
68 
69        this.getWindow().setBackgroundDrawable(grad);
70 
71    }
72 
73}

五、ShowSerView.java类的代码:

01package com.cn.daming;
02 
03
04 
05import android.app.Activity;
06 
07import android.graphics.Color;
08 
09import android.graphics.drawable.GradientDrawable;
10 
11import android.graphics.drawable.GradientDrawable.Orientation;
12 
13import android.os.Bundle;
14 
15import android.widget.TextView;
16 
17
18 
19public class ShowSerView extendsActivity {
20 
21    privateTextView ser_text_view;
22 
23
24 
25    @Override
26 
27    protectedvoid onCreate(Bundle savedInstanceState) {
28 
29        super.onCreate(savedInstanceState);
30 
31        setContentView(R.layout.show_ser_view);
32 
33        drawBackground();
34 
35
36 
37        ser_text_view = (TextView) findViewById(R.id.ser_text_view);
38 
39        MainActivity s_activity = (MainActivity) getIntent()
40 
41                .getSerializableExtra(MainActivity.SER_KEY);
42 
43        ser_text_view.setText("You name is: "+ s_activity.getS_name() + "\n"
44 
45                +"You age is: " + s_activity.getS_number() +"\n"
46 
47                +"You sex is: " + s_activity.getS_sex());
48 
49    }
50 
51
52 
53    publicvoid drawBackground() {
54 
55        GradientDrawable grad =new GradientDrawable(Orientation.TL_BR,
56 
57                newint[] { Color.rgb(0,0, 127), Color.rgb(0,0, 255),
58 
59                        Color.rgb(127,0, 255), Color.rgb(127,127, 255),
60 
61                        Color.rgb(127,255, 255), Color.rgb(255,255, 255) });
62 
63
64 
65        this.getWindow().setBackgroundDrawable(grad);
66 
67    }
68 
69}

xml布局文件

一、main.xml布局文件:

01<?xmlversion="1.0"encoding="utf-8"?>
02 
03<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
04 
05    android:orientation="vertical"
06 
07    android:layout_width="fill_parent"
08 
09    android:layout_height="fill_parent"
10 
11    >
12 
13    <TextView
14 
15        android:layout_width="fill_parent"
16 
17        android:layout_height="wrap_content"
18 
19        android:layout_gravity="center_horizontal"
20 
21        android:gravity="center_horizontal"
22 
23        android:text="@string/hello"
24 
25        android:textSize="12pt"
26 
27        />
28 
29     <Button
30 
31        android:id="@+id/list_button"
32 
33        android:layout_width="fill_parent"
34 
35        android:layout_height="wrap_content"
36 
37        android:layout_gravity="center_horizontal"
38 
39        android:gravity="center_horizontal"
40 
41        android:layout_marginTop="10dip"
42 
43        android:text="Intent传递list"
44 
45        android:textSize="12pt"
46 
47        />
48 
49     <Button
50 
51        android:id="@+id/par_button"
52 
53        android:layout_width="fill_parent"
54 
55        android:layout_height="wrap_content"
56 
57        android:layout_marginTop="10dip"
58 
59        android:layout_gravity="center"
60 
61        android:text="parcelable传递对象"
62 
63        android:textSize="12pt"
64 
65        />
66 
67     <Button
68 
69        android:id="@+id/ser_button"
70 
71        android:layout_width="fill_parent"
72 
73        android:layout_height="wrap_content"
74 
75        android:layout_gravity="center_horizontal"
76 
77        android:gravity="center_horizontal"
78 
79        android:layout_marginTop="10dip"
80 
81        android:text="serializable传递对象"
82 
83        android:textSize="12pt"
84 
85        />
86 
87</LinearLayout>

二、show_list_view.xml布局文件:

01<?xmlversion="1.0"encoding="utf-8"?>
02 
03<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
04 
05    android:orientation="vertical"
06 
07    android:layout_width="fill_parent"
08 
09    android:layout_height="fill_parent"
10 
11    >
12 
13     <TextView
14 
15        android:layout_width="fill_parent"
16 
17        android:layout_height="wrap_content"
18 
19        android:layout_marginBottom="5dip"
20 
21        android:layout_gravity="center_horizontal"
22 
23        android:gravity="center_horizontal"
24 
25        android:text="传递过来的ArrayList的值"
26 
27        android:textSize="12pt"
28 
29        />
30 
31     <TextView
32 
33        android:id="@+id/list_text_view"
34 
35        android:layout_width="fill_parent"
36 
37        android:layout_height="wrap_content"
38 
39        android:layout_marginBottom="10dip"
40 
41        android:layout_gravity="center_horizontal"
42 
43        android:gravity="center_horizontal"
44 
45        android:textSize="12pt"
46 
47        />
48 
49</LinearLayout>

三、show_par_view.xml布局文件:

01<?xmlversion="1.0"encoding="utf-8"?>
02 
03<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
04 
05    android:orientation="vertical"
06 
07    android:layout_width="fill_parent"
08 
09    android:layout_height="fill_parent"
10 
11    >
12 
13    <TextView
14 
15        android:layout_width="fill_parent"
16 
17        android:layout_height="wrap_content"
18 
19        android:layout_gravity="center_horizontal"
20 
21        android:layout_marginBottom="10dip"
22 
23        android:gravity="center_horizontal"
24 
25        android:text="接受从MainActivity中传递过来的对象"
26 
27        android:textSize="12pt"
28 
29        />
30 
31     <TextView
32 
33        android:layout_width="fill_parent"
34 
35        android:layout_height="wrap_content"
36 
37        android:layout_marginBottom="5dip"
38 
39        android:layout_gravity="center_horizontal"
40 
41        android:gravity="center_horizontal"
42 
43        android:text="传递过来的Parcelable值"
44 
45        android:textSize="12pt"
46 
47        />
48 
49     <TextView
50 
51        android:id="@+id/par_text_view"
52 
53        android:layout_width="fill_parent"
54 
55        android:layout_height="wrap_content"
56 
57        android:layout_gravity="center_horizontal"
58 
59        android:gravity="center_horizontal"
60 
61        android:textSize="12pt"
62 
63        />
64 
65</LinearLayout>

四、show_ser_view.xml布局文件:

01<?xmlversion="1.0"encoding="utf-8"?>
02 
03<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
04 
05    android:orientation="vertical"
06 
07    android:layout_width="fill_parent"
08 
09    android:layout_height="fill_parent"
10 
11    >
12 
13     <TextView
14 
15        android:layout_width="fill_parent"
16 
17        android:layout_height="wrap_content"
18 
19        android:layout_marginBottom="5dip"
20 
21        android:layout_gravity="center_horizontal"
22 
23        android:gravity="center_horizontal"
24 
25        android:text="传递过来的Serializable值"
26 
27        android:textSize="12pt"
28 
29        />
30 
31     <TextView
32 
33        android:id="@+id/ser_text_view"
34 
35        android:layout_width="fill_parent"
36 
37        android:layout_height="wrap_content"
38 
39        android:layout_marginBottom="10dip"
40 
41        android:layout_gravity="center_horizontal"
42 
43        android:gravity="center_horizontal"
44 
45        android:textSize="12pt"
46 
47        />
48 
49</LinearLayout>

Manifest.xml布局文件:

01<?xmlversion="1.0"encoding="utf-8"?>
02 
03<manifestxmlns:android="http://schemas.android.com/apk/res/android"
04 
05    package="com.cn.daming"android:versionCode="1"android:versionName="1.0">
06 
07    <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
08 
09        <activityandroid:name=".MainActivity"android:label="@string/app_name">
10 
11            <intent-filter>
12 
13                <actionandroid:name="android.intent.action.MAIN"/>
14 
15                <categoryandroid:name="android.intent.category.LAUNCHER"/>
16 
17            </intent-filter>
18 
19        </activity>
20 
21        <activityandroid:name=".ShowListView"/>
22 
23        <activityandroid:name=".ShowParView"/>
24 
25        <activityandroid:name=".ShowSerView"/>
26 
27    </application>
28 
29    <uses-sdkandroid:minSdkVersion="8"/>
30 
31

</manifest>

 

 

 

 

 

 

 


android 中 自定义的对象序列化的问题有两个选择一个是Parcelable,另外一个是Serializable。

一 序列化原因:

1.永久性保存对象,保存对象的字节序列到本地文件中;
2.通过序列化对象在网络中传递对象;
3.通过序列化在进程间传递对象。

二 至于选取哪种可参考下面的原则:

1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。


实现:
1 Serializable 的实现,只需要继承  implements Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。

2 Parcelabel 的实现,需要在类中添加一个静态成员变量 CREATOR,这个变量需要继承 Parcelable.Creator 接口。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值