Activity之间传递类对象

http://hi.baidu.com/nubone/blog/item/53a21974f98c3117b051b9ba.html

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

传递类对象以前一直没搞懂怎么做,都是用静态引用。现在终于搞懂了,实现Serializable接口,废话少说,看代码。

传递的类

  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. }  

发送部分

  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);  

接收部分

  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()));  

搞定,打完收工。。。



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值