Activity传递一个或者多个对象

Activity之间传递对象,或者通过Bundle传递对象的两种方式。

1:Serializable方式
  传递一个对象

2:Parcelable方式
  传递一个对象、传递多个对象(ArrayList<Object>)


方式一:Serializable

     传递类:
    
Java代码   收藏代码
  1. public class CustomeClass implements Serializable{  
  2.       
  3.     /** 
  4.      *  
  5.      */  
  6.     private static final long serialVersionUID = -7060210544600464481L;  
  7.     private String name;  
  8.     private String id;  
  9.     private int age;  
  10.     private String sex;  
  11.       
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     public String getId() {  
  19.         return id;  
  20.     }  
  21.     public void setId(String id) {  
  22.         this.id = id;  
  23.     }  
  24.     public int getAge() {  
  25.         return age;  
  26.     }  
  27.     public void setAge(int age) {  
  28.         this.age = age;  
  29.     }  
  30.     public String getSex() {  
  31.         return sex;  
  32.     }  
  33.     public void setSex(String sex) {  
  34.         this.sex = sex;  
  35.     }  
  36.   
  37. }  



       发送部分:
      
Java代码   收藏代码
  1. CustomeClass cc = new CustomeClass();  
  2. cc.setAge(21);  
  3. cc.setId("123456");  
  4. cc.setName("mingkg21");  
  5. cc.setSex("男");  
  6.   
  7. Intent intent = new Intent(this, PersonInfo.class);  
  8. intent.putExtra("PERSON_INFO", cc);  
  9. startActivity(intent);  


       
       接收部分:
      
Java代码   收藏代码
  1.        Intent intent = getIntent();  
  2. CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO");  
  3. setTextView(R.id.id, cc.getId());  
  4. setTextView(R.id.name, cc.getName());  
  5. setTextView(R.id.sex, cc.getSex());  
  6. setTextView(R.id.age, String.valueOf(cc.getAge()));  



方式二:Parcelable

     传递类:
    
Java代码   收藏代码
  1. public class CustomeParcelable implements Parcelable {  
  2.       
  3.     private String name;  
  4.     private String id;  
  5.     private int age;  
  6.     private String sex;  
  7.   
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.   
  12.     public void setName(String name) {  
  13.         this.name = name;  
  14.     }  
  15.   
  16.     public String getId() {  
  17.         return id;  
  18.     }  
  19.   
  20.     public void setId(String id) {  
  21.         this.id = id;  
  22.     }  
  23.   
  24.     public int getAge() {  
  25.         return age;  
  26.     }  
  27.   
  28.     public void setAge(int age) {  
  29.         this.age = age;  
  30.     }  
  31.   
  32.     public String getSex() {  
  33.         return sex;  
  34.     }  
  35.   
  36.     public void setSex(String sex) {  
  37.         this.sex = sex;  
  38.     }  
  39.       
  40.     public static final Parcelable.Creator<CustomeParcelable> CREATOR = new Creator<CustomeParcelable>(){  
  41.   
  42.         public CustomeParcelable createFromParcel(Parcel source) {  
  43.             // TODO Auto-generated method stub  
  44.             CustomeParcelable cus = new CustomeParcelable();  
  45.             cus.name = source.readString();  
  46.             cus.id = source.readString();  
  47.             cus.age = source.readInt();  
  48.             cus.sex = source.readString();  
  49.             return cus;  
  50.         }  
  51.   
  52.         public CustomeParcelable[] newArray(int size) {  
  53.             // TODO Auto-generated method stub  
  54.             return new CustomeParcelable[size];  
  55.         }  
  56.           
  57.     };  
  58.   
  59.     public int describeContents() {  
  60.         // TODO Auto-generated method stub  
  61.         return 0;  
  62.     }  
  63.   
  64.     public void writeToParcel(Parcel dest, int flags) {  
  65.         // TODO Auto-generated method stub  
  66.         dest.writeString(name);  
  67.         dest.writeString(id);  
  68.         dest.writeInt(age);  
  69.         dest.writeString(sex);  
  70.     }  
  71.   
  72. }  


       发送部分:
      
Java代码   收藏代码
  1. CustomeParcelable cc = new CustomeParcelable();  
  2. cc.setAge(21);  
  3. cc.setId("123456");  
  4. cc.setName("mingkg21");  
  5. cc.setSex("男");  
  6.           
  7. Intent intent = new Intent(this, PersonInfo.class);  
  8. intent.putExtra("PERSON_INFO", cc);  
  9. startActivity(intent);  


        接受部分:
       
Java代码   收藏代码
  1. Intent intent = getIntent();  
  2. CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO");  
  3. setTextView(R.id.id, cc.getId());  
  4. setTextView(R.id.name, cc.getName());  
  5. setTextView(R.id.sex, cc.getSex());  
  6. setTextView(R.id.age, String.valueOf(cc.getAge()));  



以上为Parcelable传递一个对象,若要实现传递多个对象,
传递部分:
Java代码   收藏代码
  1. Bundle bundle = new Bundle();  
  2.         bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs);  
  3.         msg.setData(bundle);  
  4.         endDocNotice.sendMessage(msg);  


接受部分:
Java代码   收藏代码
  1. Bundle bundle =  msg.getData();  
  2.                 mP3TagForNetDTOs = bundle.getParcelableArrayList("mP3TagForNetDTOs");  


此文转载,来自: http://mingkg21.iteye.com/blog/438913
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值