Intent传递List和Object和List<Object>(附源码)(转)

来源:互联网 发布:淘宝海关拍卖车可靠吗 编辑:程序博客网 时间:2024/06/07 01:35

 二、传递Object

有两种方式来传递Object:Serializable和Parcelable

2.1 使用Serializable方式

前提:Object需要实现Serializable接口
用Serializable方式传递Object的语法:bundle.putSerializable(key,object);
用Serializable方式接收Object的语法:object=(Object) getIntent().getSerializableExtra(key);
实现Serializable接口就是把对象序列化,然后再传输,和Java的常用编程没什么明显区别,而且Object不需要明显改变,推荐用这种方式。

Object实现Serializable

  1. package com.wirelessqa.testintent;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6.  * OBJECT实现SERIALIZABLE 
  7.  * @author bixiaopeng 2013-2-18 上午11:32:19 
  8.  */  
  9. public class SerInfo implements Serializable {  
  10.   
  11.     private String name;  
  12.     private String website;  
  13.     private String weibo;  
  14.     public SerInfo(){}  
  15.   
  16.     public SerInfo(String name, String website, String weibo){  
  17.         this.name = name;  
  18.         this.website = website;  
  19.         this.weibo = weibo;  
  20.     }  
  21.   
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.   
    1.   
    2.     public void setName(String name) {  
    3.         this.name = name;  
    4.     }  
    5.   
    6.     public String getWebsite() {  
    7.         return website;  
    8.     }  
    9.   
    10.     public void setWebsite(String website) {  
    11.         this.website = website;  
    12.     }  
    13.   
    14.     public String getWeibo() {  
    15.         return weibo;  
    16.     }  
    17.   
    18.    
  1.  public void setWeibo(String weibo) {  
  2.         this.weibo = weibo;  
  3.     }  
  4.   
  5. }  

用Serializable方式传递Object

[java] view plain copy
  1. SerInfo serInfo = new SerInfo(name, website, weibo);  
  2.                 Intent intent = new Intent();  
  3.                 Bundle bundle = new Bundle();  
  4.                 bundle.putSerializable("serinfo", serInfo);  
  5.                 intent.setClass(MainActivity.this, ResultActivity.class);  
  6.                 intent.putExtras(bundle);  
  7.                 startActivity(intent);  

用Serializable方式接收Object

[java] view plain copy
  1. //获得Serializable方式传过来的值  
  2.         SerInfo serInfo = (SerInfo) getIntent().getSerializableExtra("serinfo");  

 三、传递List<Object>

传递List<Object>的方法

[java] view plain copy
  1. ArrayList<SerInfo> listObj = new ArrayList<SerInfo>();  
  2.   
  3. SerInfo serInfo1 = new SerInfo(name, website, weibo);  
  4.   
  5. SerInfo serInfo2 = new SerInfo(name, website, weibo);  
  6.   
  7. listObj.add(serInfo1);  
  8.   
  9. listObj.add(serInfo2);  
  10.   
  11. Intent intent = new Intent();  
  1. intent.setClass(MainActivity.this, ResultActivity.class);  
  2.   
  3. intent.putExtra("listobj", (Serializable) listObj);  
  4.   
  5. startActivity(intent);  

接收List<Object>的方法

[java] view plain copy
  1. //获得传过来的List<Object>  
  2.   
  3.         ArrayList<SerInfo> listObj =  (ArrayList<SerInfo>) getIntent().getSerializableExtra("listobj");


0 0
原创粉丝点击