HashMap<String,String> map2 = new HashMap<String,String>();
map2.put("key1", "value1");
map2.put("key2", "value2");
Bundle bundleSerializable = new Bundle();
bundleSerializable.putSerializable("serializable", map2);
Intent intentSerializable = new Intent();
intentSerializable.putExtras(bundleSerializable);
intentSerializable.setClass(MainActivity.this,
SerializableActivity.class);
startActivity(intentSerializable);
3.2 接收参数
[java]
this.setTitle("Serializable例子");
//接收参数
Bundle bundle = this.getIntent().getExtras();
//如果传 LinkedHashMap,则bundle.getSerializable转换时会报ClassCastException,不知道什么原因
//传HashMap倒没有问题。
HashMap<String,String> map = (HashMap<String,String>)bundle.getSerializable("serializable");
String sResult = "map.size() ="+map.size();
Iterator iter = map.entrySet().iterator();
while(iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
sResult +="\r\n key----> "+(String)key;
sResult +="\r\n value----> "+(String)value;
}
4. 通过实现Parcelable接口
这个是通过实现Parcelable接口,把要传的数据打包在里面,然后在接收端自己分解出来。这个是Android独有的,在其本身的
源码中也用得很多,
效率要比Serializable相对要好。
4.1 首先要定义一个类,用于 实现Parcelable接口
因为其本质也是序列化数据,所以这里要注意定义顺序要与解析顺序要一致噢。
[java]
public class XclParcelable implements Parcelable {
//定义要被传输的数据
public int mInt;
public String mStr;
public HashMap<String,String> mMap = new HashMap<String,String>();
//Describe the kinds of special objects contained in this Parcelable's marshalled representation.
public int describeContents() {
return 0;
}
//Flatten this object in to a Parcel.
public void writeToParcel(Parcel out, int flags) {
//等于将数据映射到Parcel中去
out.writeInt(mInt);
out.writeString(mStr);
out.writeMap(mMap);
}
//Interface that must be implemented and provided as a public CREATOR field
//that generates instances of your Parcelable class from a Parcel.
public static final Parcelable.Creator<XclParcelable> CREATOR
= new Parcelable.Creator<XclParcelable>() {
public XclParcelable createFromParcel(Parcel in) {
return new XclParcelable(in);
}
public XclParcelable[] newArray(int size) {
return new XclParcelable[size];
}
};
private XclParcelable(Parcel in) {
//将映射在Parcel对象中的数据还原回来
//警告,这里顺序一定要和writeToParcel中定义的顺序一致才行!!!
mInt = in.readInt();
mStr = in.readString();
mMap = in.readHashMap(HashMap.class.getClassLoader());
}
public XclParcelable() {
// TODO Auto-generated constructor stub
}
}
4.2 设置参数
[java]
//通过实现Parcelable接口传参的例子
Intent intentParcelable = new Intent();
XclParcelable xp = new XclParcelable();
xp.mInt = 1;
xp.mStr = "字符串";
xp.mMap = new HashMap<String,String>();
xp.mMap.put("key", "value");
intentParcelable.putExtra("Parcelable", xp);
intentParcelable.setClass(MainActivity.this,
ParcelableActivity.class);
startActivity(intentParcelable);
4.3 接收参数
[java]
<span style="white-space:pre"> </span>this.setTitle("Parcelable例子");
//接收参数
Intent i = getIntent();
XclParcelable xp = i.getParcelableExtra("Parcelable");
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText( " mInt ="+xp.mInt
+"\r\n mStr"+xp.mStr
+"\r\n size()="+xp.mMap.size());
5. 通过单例模式实现参数传递
单例模式的特点就是可以保证
系统中一个类有且只有一个实例。这样很容易就能实现,
在A中设置参数,在B中直接访问了。这是几种方法中效率最高的。
5.1 定义一个单实例的类
[java]
//单例模式
public class XclSingleton
{
//单例模式实例
private static XclSingleton instance = null;
//synchronized 用于线程安全,防止多线程同时创建实例
public synchronized static XclSingleton getInstance(){
if(instance == null){
instance = new XclSingleton();
}
return instance;
}
final HashMap<String, Object> mMap;
public XclSingleton()
{
mMap = new HashMap<String,Object>();
}
public void put(String key,Object value){
mMap.put(key,value);
}
public Object get(String key)
{
return mMap.get(key);
}
}
5.2 设置参数
[java]
//通过单例模式传参数的例子
XclSingleton.getInstance().put("key1", "value1");
XclSingleton.getInstance().put("key2", "value2");
Intent intentSingleton = new Intent();
intentSingleton.setClass(MainActivity.this,
SingletonActivity.class);
startActivity(intentSingleton);
5.3 接收参数
[java]
<span style="white-space:pre"> </span>this.setTitle("单例模式例子");
//接收参数
HashMap<String,Object> map = XclSingleton.getInstance().mMap;
String sResult = "map.size() ="+map.size();
//遍历参数
Iterator iter = map.entrySet().iterator();
while(iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
sResult +="\r\n key----> "+(String)key;
sResult +="\r\n value----> "+(String)value;
}