分享

Android中Intent传递对象的两种方法(Serializable,Parcelab...

 myrepository 2012-04-12

Android中Intent传递对象的两种方法(Serializable,Parcelable)

分类: Android 开发 181人阅读 评论(0) 收藏 举报
 

Activity之间通过Intent传递值,支持基本数据类型和String对象及它们的数组对象byte、byte[]、char、char[]、boolean、boolean[]、short、short[]、int、int[]、long、long[]、float、float[]、double、double[]、String、String[],还有实现Serializable、Parcelable接口的类对象。

对于,Serializable 还比较熟悉,但是,对于 Parcelable 这个东东,它是android 的一个新的序列化方式。(下面会详细的讲解Parcelable )

下面,将给出用Serializable 和 Parcelable 这两种方式来传递对象的示例代码:

      

        *下面的是传递的对象 -----------

/***
 * 要传递的对象Serializable 
 * @author Administrator
 *
 */
public class PassObject_Serializable implements Serializable{
 
 public PassObject_Serializable() {
  super();
 }

 public PassObject_Serializable(int id, String name, double price) {
  super();
  this.id = id;
  this.name = name;
  this.price = price;
 }

 private int id;
 
 private String name ;
 
 private double price;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public double getPrice() {
  return price;
 }

 public void setPrice(double price) {
  this.price = price;
 }
}

 

/***
 * 要传递的对象Parcelable
 * @author Administrator
 *
 */
public class PassObject_Parcelable implements Parcelable{
 
 @Override
 public int describeContents() {
  return 0;
 }

 @Override
 public void writeToParcel(Parcel dest, int flags) {
  dest.writeInt(this.id);
  dest.writeString(this.name);
  dest.writeDouble(this.price);
 }
 public static final Parcelable.Creator<PassObject_Parcelable> CREATOR = new Creator<PassObject_Parcelable>() {
  @Override
  public PassObject_Parcelable[] newArray(int size) {
   
   return new PassObject_Parcelable[size];
  }
  @Override
  public PassObject_Parcelable createFromParcel(Parcel source) {
   PassObject_Parcelable temp = new PassObject_Parcelable();
   
   temp.id = source.readInt();
   temp.name = source.readString();
   temp.price = source.readDouble();
   return temp;
  }
 };
 
 public PassObject_Parcelable() {
  super();
  // TODO Auto-generated constructor stub
 }

 public PassObject_Parcelable(int id, String name, double price) {
  super();
  this.id = id;
  this.name = name;
  this.price = price;
 }
 private int id;
 
 private String name ;
 
 private double price;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public double getPrice() {
  return price;
 }

 public void setPrice(double price) {
  this.price = price;
 }
 
}

 

* 以上是两个要传递的对象,一个实现 Serializable ,一个实现 Parcelable

* 注意 ,红色的部分

*下面的是传递对象的activity -----------

/**
 *  传递对象的页面A
 * @author Administrator
 *
 */
public class ActivityAextends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_a);
       
        Button btnSerlizableBtn = (Button)ActivityA.this.findViewById(R.id.go_btn1);
        Button btnParcelableBtn = (Button)ActivityA.this.findViewById(R.id.go_btn2);
        btnSerlizableBtn.setOnClickListener(new mybtnClick(true, new PassObject_Serializable(1,"牛奶", 50.0)));
        btnParcelableBtn.setOnClickListener(new mybtnClick(false, new PassObject_Parcelable(1,"牛奶", 50.0)));
       
    }
   
    /**
     * 单击类
     */
    private class mybtnClick implements OnClickListener {
     private boolean  type = false;  //true 为 Serializable false 为Parcelable
     private Object passObj =null;   //要传递的对象
  public mybtnClick(boolean type, Object passObj) {
   super();
   this.type = type;
   this.passObj = passObj;
  }  @Override
  public void onClick(View v) {
   Bundle mybundle = new Bundle();
   mybundle.putBoolean("type",type);
   if(type){
    mybundle.putSerializable("obj",(Serializable) passObj ); 
   }else{
    mybundle.putParcelable("obj",(Parcelable) passObj);
   }
   Intent myintent = new Intent();
   myintent.putExtras(mybundle);
   myintent.setClass(ActivityA.this, ActivityB.class);
   startActivity(myintent);
  }
    }
}

 

 

/**
 * 接受对象的页面B
 * @author Administrator
 *
 */
public class ActivityB extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.page_b);
  
  TextView txt = (TextView)ActivityB.this.findViewById(R.id.txt1);
  Bundle myBundle = this.getIntent().getExtras();
  if(myBundle!=null){
   
   Object obj = null;
   
   boolean type = myBundle.getBoolean("type");
   if(type){
    obj =(PassObject_Serializable) myBundle.getSerializable("obj");
    
   }else{
    obj =(PassObject_Parcelable) myBundle.getParcelable("obj");
   }
   StringBuffer str = new StringBuffer();
   str.append("类型:"+(type?"Serializable":"Parcelable"));
   str.append("\n");
   String id,name,price;
   if(type){
    id = ((PassObject_Serializable)obj).getId()+"";
    name = ((PassObject_Serializable)obj).getName()+"";
    price = ((PassObject_Serializable)obj).getPrice()+"";
   }else{
    id = ((PassObject_Parcelable)obj).getId()+"";
    name = ((PassObject_Parcelable)obj).getName()+"";
    price = ((PassObject_Parcelable)obj).getPrice()+"";
   }
   str.append("id:"+id+"\n");
   str.append("name:"+name+"\n");
   str.append("price:"+price+"\n");
   txt.setText(str.toString());
  }
 }
}

 

* 以上就是,传递对象的两种序列化方式,如果你还是不明白,欢迎下载demo程序,以下是链接:

http://download.csdn.net/detail/zjl5211314/3798905 

 

 

 

* 头开始,提到了Parcelable ,是android的一种新的序列化方式,那现在就了解下她吧。 

android提供了一种新的类型:Parcel。本类被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。

除了基本类型以外,只有实现了Parcelable接口的类才能被放入Parcel中。

Parcelable实现要点:需要实现三个东西

1)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.声明如下:

writeToParcel (Parcel dest, int flags) 具体参数含义见 javadoc

2)describeContents方法。没搞懂有什么用,反正直接返回0也可以

3)静态的Parcelable.Creator接口,本接口有两个方法:

createFromParcel(Parcel in) 实现从in中创建出类的实例的功能

newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。估计本方法是供外部类反序列化本类数组使用。

 

*该段落,引自百度文档。

通过使用 Parcelable ,

1. writeToParcel  方法 ,主要是将数据写入 parcle 中;

2.Parcelable.Creator 类中的 createFromParcel(Parcel source) 方法,主要是,从 parcle  中读取数据;

3. describeContents 方法,基本不用动,直接返回0 就可以;

4.Parcelable.Creator 类中的 newArray(int size)  方法,没怎么体会过。

5.总之,个人,感觉用这个Parcelable  序列化,是通过 parcle   来传递数据的。

(目前,感受就这些,以后可能会更了解她得其他特性,到时候,会继续完善下 *_* )

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多