【转】Android中如何使用Bundle传递对象[使用Serializable或者Parcelable] -- 不错
原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1211/694.html
Android中Bundle类的作用
Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值。相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。Bundle的内部实际上是使用了HashMap类型的变量来存放putXxx()方法放入的值。
用Bundle可以在Activity中传递基本数据类型,比如int、float、string等,但是现在的需求是如何在Activity中传递对象实例呢?就我目前所知的有两种,分别是java中Serializable和Android新引进的Parcelable方法。
我们可以考虑采用Bundle.putSerializable(Key,Object);也可以考虑采用Bundle.putParcelable(Key, Object);其中前面一种方法中的Object必须要实现Serializable接口,后面一种方法中的Object必须要实现Parcelable接口。如果没有实现这两个接口之一,那么我们可以使用Bundle.putSerializable(Key,Object)或者Bundle.putParcelable(Key, Object),但起不到任何作用,下面我们以一个完整的例子来说明。
1.新建一个Android的工程,其中该工程的目录结构如下图:

2. 修改main.xml布局文件。布局文件的源码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/serButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Serializable"/> <Button android:id="@+id/parButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Parcelable"/> </LinearLayout> |
3.在工程的src目录下新建一个实体类包,命名为com.andy.entity.同时在该package中添加两个实体类,一个是Person.java,该类实现Serializable接口;一个是Police.java,该类实现Parcelable接口。代码分别如下:
Person.java:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.andy.entity; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = -6919461967497580385L; private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
Police.java:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.andy.entity; import android.os.Parcel; import android.os.Parcelable; public class Police implements Parcelable { private String name; private int workTime; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getWorkTime() { return workTime; } public void setWorkTime(int workTime) { this.workTime = workTime; } public static final Parcelable.Creator<Police> CREATOR = new Creator<Police>() { @Override public Police createFromParcel(Parcel source) { Police police = new Police(); police.name = source.readString(); police.workTime = source.readInt(); return police; } @Override public Police[] newArray(int size) { return new Police[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int flags) { parcel.writeString(name); parcel.writeInt(workTime); } } |
4.在包com.andy.testdemo中修改TestActivity.java类,同时在该包中添加类SerializableDemo和ParcelableDemo,分别继承了Activity类和分别显示Person对象和Police对象的数据。代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package com.andy.testdemo; import com.andy.entity.Person; import com.andy.entity.Police; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class TestActivity extends Activity { private Button sButton,pButton; public final static String SER_KEY = "com.andy.ser"; public final static String PAR_KEY = "com.andy.par"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sButton = (Button)findViewById(R.id.serButton); sButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SerializeMethod(); } }); pButton = (Button)findViewById(R.id.parButton); pButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PacelableMethod(); } }); } /** * Serializeable传递对象的方法 */ private void SerializeMethod(){ Person mPerson = new Person(); mPerson.setName("andy"); mPerson.setAge(26); Intent mIntent = new Intent(this,SerializableDemo.class); Bundle mBundle = new Bundle(); mBundle.putSerializable(SER_KEY,mPerson); mIntent.putExtras(mBundle); startActivity(mIntent); } /** * Pacelable传递对象方法 */ private void PacelableMethod(){ Police mPolice = new Police(); mPolice.setName("I am Police"); mPolice.setWorkTime(2008); Intent mIntent = new Intent(this,ParcelableDemo.class); Bundle mBundle = new Bundle(); mBundle.putParcelable(PAR_KEY, mPolice); mIntent.putExtras(mBundle); startActivity(mIntent); } } |
SerializableDemo.java类
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.andy.testdemo; import com.andy.entity.Person; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class SerializableDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Person mPerson = (Person)getIntent().getSerializableExtra(TestActivity.SER_KEY); mTextView.setText("You name is: " + mPerson.getName() + "/n"+ "You age is: " + mPerson.getAge()); setContentView(mTextView); } } |
ParcelableDemo.java类:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.andy.testdemo; import com.andy.entity.Police; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ParcelableDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Police mPolice = (Police)getIntent().getParcelableExtra(TestActivity.PAR_KEY); mTextView.setText("Police name is: " + mPolice.getName()+"/n"+ "WorkTime is: " + mPolice.getWorkTime() + "/n"); setContentView(mTextView); } } |
5.在AndroidManifest.xml文件中为新添加的两个Activity进行注册。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.andy.testdemo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TestActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SerializableDemo"/> <activity android:name=".ParcelableDemo"/> </application> <uses-sdk android:minSdkVersion="8" /> </manifest> |
【转】Android中如何使用Bundle传递对象[使用Serializable或者Parcelable] -- 不错的更多相关文章
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
http://blog.csdn.net/cjjky/article/details/6441104 在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSeri ...
- Android中,利用Intent传递对象值
在很多情况下,调用startActivity(Intent) 方法,跳转到另外一个Activity或其他component,需要传递一个对象给它. 可以让这个要传递的对象所属类实现Serializab ...
- Android中的Parcel机制 实现Bundle传递对象
Android中的Parcel机制 实现了Bundle传递对象 使用Bundle传递对象,首先要将其序列化,但是,在Android中要使用这种传递对象的方式需要用到Android Parc ...
- 在Android中通过Intent使用Bundle传递对象
IntentBundle传递对象SerializableParcelable Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法.被传递的对象 ...
- Android Bundle传递对象
首先Android的Bundle是可以传递对象的.我们可以用Bundle b = new Bundle():b.putSerializable("key", 对象引用); 但是这样 ...
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
一般情况下,客户端和服务端的数据交互都是使用json和XML,相比于XML,json更加轻量级,并且省流量,但是,无论我们用json还是用xml,都需要我们先将数据封装成json字符串或者是一个xml ...
- Android中利用C++处理Bitmap对象
相信有些Android&图像算法开发者和我一样,遇到过这样的状况:要对Bitmap对象做一些密集计算(例如逐像素的滤波),但是在java层写循环代码来逐像素操作明显是不现实的,因为Java代码 ...
- android中handler和bundle有什么区别和联系 都是用来传递消息吗都是信息的载体吗
1.handler是消息处理者,通常重写Handler的handleMessage()方法,在方法中处理接收到的不同消息,例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Ha ...
- jquery中ajax向action传递对象参数,json ,spring注入对象
首先,我这个程序的框架是spring+struts2+hibernate. 后端的action的需要接受从前端传进来的参数,由spring的注入,可知,如果前端用的是form的话,只需要在每个inpu ...
随机推荐
- WKWebView详解
WKWebView是在Apple的WWDC 2014随iOS 8和OS X 10.10出来的,是为了解决UIWebView加载速度慢.占用内存大的问题.使用UIWebView加载网页的时候,我们会发现 ...
- LevelDB windows vs2013 c++编译和测试
引用: (src1) :http://download.csdn.net/detail/flyfish1986/8881263(这里有下载地址) (src2) :http://blog.csdn.ne ...
- 02_Jquery_03_类选择器
[简述] 类选择器就是通过类名(css类名)来查询元素! $(".myClass")就可以把所有包含了class="myClass"的元素查询出来 [index ...
- Linux下U盘的格式化
一次系统装机带来的烦恼. 之前有一次装centos 系统 ,把一个centos4.8的系统刻录到了一个8G的U盘,之后是centos安装成功了 ,却发现电脑不认识U盘了,试了好多次也没有处理好,刚好今 ...
- redis php 实例
redis php 实例一 redis的操作很多的,以前看到一个比较全的博客,但是现在找不到了.查个东西搜半天,下面整理一下php处理redis的例子,个人觉得常用一些例子.下面的例子都是基于php- ...
- jquery 去掉重复项(splice,apply,push)
/* js数组去掉重复项 var somearray = [1,1,2,2,3,3,4,4,'1']; somearray.check(); //somearray will return arr=[ ...
- php计算最后一次,第一次字符串出现位置
strpos($str, n) 首次,n在str第一次出现位置, strrpos($str, n) 最后一次,n在str最后一次出现位置 strripos区分大小写
- mysql扩展库操作mysql数据库
环境搭建 启用mysql扩展库,在php.ini文件中去配置mysql扩展库 extension=php_mysql.dll 查询数据库 1.建库建表 //建库testcreate database ...
- Redis的PHP操作手册(自用)
String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 $redis-> ...
- JS 页面加载触发事件 document.ready和onload的区别(转)
原博地址:http://blog.163.com/zhaoyanping_1125/blog/static/20132915320111129113723710/ * document.ready和o ...