Intent传递List和Object和List<Object>
一、传递List
传递List<String>的方法
小技巧,List<object> 可以使用json 转为 List<string>,就可以使用 List<string>方法了
- ArrayList<String> info = new ArrayList<String>();
- info.add(name);
- info.add(website);
- info.add(weibo);
- Intent intent = new Intent(MainActivity.this, ResultActivity.class);
- intent.putStringArrayListExtra("infoList", info);
- startActivity(intent);
接收List<String>的方法
- ArrayList<String> infoList = new ArrayList<String>();
- infoList = getIntent().getStringArrayListExtra("infoList");
传递List<Integer>的方法
- intent.putIntegerArrayListExtra(key, list);
接收List<Integer>的方法
- list =(ArrayList<Integer>) getIntent().getIntegerArrayListExtra(key);
二、传递Object
有两种方式来传递Object:Serializable和Parcelable
2.1 使用Serializable方式
Object实现Serializable
- package com.wirelessqa.testintent;
- import java.io.Serializable;
- /**
- * OBJECT实现SERIALIZABLE
- * @author bixiaopeng 2013-2-18 上午11:32:19
- */
- public class SerInfo implements Serializable {
- private String name;
- private String website;
- private String weibo;
- public SerInfo(){}
- public SerInfo(String name, String website, String weibo){
- this.name = name;
- this.website = website;
- this.weibo = weibo;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getWebsite() {
- return website;
- }
- public void setWebsite(String website) {
- this.website = website;
- }
- public String getWeibo() {
- return weibo;
- }
- public void setWeibo(String weibo) {
- this.weibo = weibo;
- }
- }
用Serializable方式传递Object
- SerInfo serInfo = new SerInfo(name, website, weibo);
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- bundle.putSerializable("serinfo", serInfo);
- intent.setClass(MainActivity.this, ResultActivity.class);
- intent.putExtras(bundle);
- startActivity(intent);
用Serializable方式接收Object
- //获得Serializable方式传过来的值
- SerInfo serInfo = (SerInfo) getIntent().getSerializableExtra("serinfo");
2.2 使用Parcelable方式
实现Parcelable接口的类比较复杂,Parcelable是个什么东西呢?
实现Parcelable接口需要实现三个方法: 1)writeToParcel方法。该方法将类的数据写入外部提供的Parcel中。
声明:writeToParcel(Parcel dest, int flags)。
2)describeContents方法。直接返回0就可以。
3)静态的Parcelable.Creator<T>接口,本接口有两个方法:createFromParcel(Parcel in) 实现从in中创建出类的实例的功能。
Object需要实现Parcelable接口
- package com.wirelessqa.testintent;
- import android.os.Parcel;
- import android.os.Parcelable;
- /**
- * Object需要实现Parcelable接口
- * @author bixiaopeng 2013-2-18 上午11:32:19
- */
- public class ParInfo implements Parcelable{
- private String name;
- private String website;
- private String weibo;
- public ParInfo(){
- }
- public ParInfo(String name, String website, String weibo){
- this.name = name;
- this.website = website;
- this.weibo = weibo;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getWebsite() {
- return website;
- }
- public void setWebsite(String website) {
- this.website = website;
- }
- public String getWeibo() {
- return weibo;
- }
- public void setWeibo(String weibo) {
- this.weibo = weibo;
- }
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- //该方法将类的数据写入外部提供的Parcel中。
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeString(name);
- dest.writeString(website);
- dest.writeString(weibo);
- }
- public static final Parcelable.Creator<ParInfo> CREATOR = new Creator<ParInfo>() {
- //实现从source中创建出类的实例的功能
- @Override
- public ParInfo createFromParcel(Parcel source) {
- ParInfo parInfo = new ParInfo();
- parInfo.name = source.readString();
- parInfo.website= source.readString();
- parInfo.weibo = source.readString();
- return parInfo;
- }
- //创建一个类型为T,长度为size的数组
- @Override
- public ParInfo[] newArray(int size) {
- return new ParInfo[size];
- }
- };
- }
用Parcelable方式传递Object
- ParInfo parInfo = new ParInfo(name, website, weibo);
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- bundle.putParcelable("parinfo", parInfo);// 这里调用了writeToParcel方法,向dest写数据
- intent.setClass(MainActivity.this, ResultActivity.class);
- intent.putExtras(bundle);
- startActivity(intent);
用Parcelable方式接收Object
- //获得Parcelable方式传过来的值
- ParInfo parInfo = (ParInfo) getIntent().getParcelableExtra("parinfo");//这里调用了UsercreateFromParcel方法,返回ParInfo实例
三、传递List<Object>
传递List<Object>的方法
- ArrayList<SerInfo> listObj = new ArrayList<SerInfo>();
- SerInfo serInfo1 = new SerInfo(name, website, weibo);
- SerInfo serInfo2 = new SerInfo(name, website, weibo);
- listObj.add(serInfo1);
- listObj.add(serInfo2);
- Intent intent = new Intent();
- intent.setClass(MainActivity.this, ResultActivity.class);
- intent.putExtra("listobj", (Serializable) listObj);
- startActivity(intent);
接收List<Object>的方法
- //获得传过来的List<Object>
- ArrayList<SerInfo> listObj = (ArrayList<SerInfo>) getIntent().getSerializableExtra("listobj");
- <span style="font-size:24px; color:#ff0000">源码下载:http://pan.baidu.com/share/link?shareid=266032&uk=436271564</span>
Intent传递List和Object和List<Object>的更多相关文章
- android intent 传递list或者对象
		(转:http://www.cnblogs.com/lee0oo0/archive/2012/09/24/2699805.html) 方法一: 如果单纯的传递List<String> 或者 ... 
- Android 通过 Intent 传递类对象或list对象
		(转:http://www.cnblogs.com/shaocm/archive/2013/01/08/2851248.html) Android中Intent传递类对象提供了两种方式一种是 通过实现 ... 
- Intent传递对象的两种方法(Serializable,Parcelable)   (转)
		今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ... 
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
		今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ... 
- Android intent传递list或对象
		方法一: 如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用 Java代码 intent.putStringArrayListExtra ... 
- Android 开发中使用Intent传递数据的方法
		Activity之间通过Intent传递值,支持基本数据类型和String对象及 它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.shor ... 
- Android 通过 Intent 传递类对象
		Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ... 
- Android 开发笔记——通过 Intent 传递类对象
		Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ... 
- [Android] Android 最全 Intent 传递数据姿势
		我们都是用过 Intent,用它来在组件之间传递数据,所以说 Intent 是组件之间通信的使者,一般情况下,我们传递的都是一些比较简单的数据,并且都是基本的数据类型,写法也比较简单,今天我在这里说的 ... 
- 【转】Android 之最新最全的Intent传递数据方法
		原文地址:https://www.jianshu.com/p/1169dba99261 intent传递数据 为什么要和intent单独拿出来讲,因为Intent传递数据也是非常重要的 一.简单的传递 ... 
随机推荐
- ORA-20000 ORU-10027 buffer overflow limit of 2000 bytes
			这是在pl/sql中执行存储过程报的错,原因是serveroutput限制存储, 解决方案:set serveroutput on size 10000000; 
- 用python+selenium从百度获取本地明日的天气信息并根据温度情况设置提醒
			从百度天气获取当地明天的天气情况,如果明天下雨,请发送邮件通知全体同事带伞, 如果明天气温低于10度,请邮件提醒同事注意保暖,如果气温高于30度则提醒同事注意高温. 假设存在发送邮件的方法self.s ... 
- Mac 配置Spark环境scala+python版本(Spark1.6.0)
			1. 从官网下载Spark安装包,解压到自己的安装目录下(默认已经安装好JDK,JDK安装可自行查找): spark官网:http://spark.apache.org/downloads.html ... 
- SpringMVC中定时器继承Task后无法对service注入问题
			最近在做一个Spring+MyBatis的一个项目,其中用到了Redis的存储,然后遇到问题是这样的: RedisTask是通过定时器来每分钟像数据库里推送的,于是就有了 public class R ... 
- touch srceen
			/etc/udev/rules.d touchrules reset 
- lua随机数函数
			function rnd(max) --lua的第1次random数不靠谱,取第3次的靠谱 local ret=0 math.randomseed(os.time()) for i=1,3 d ... 
- FZU-2075 Substring(后缀数组)
			Description Given a string, find a substring of it which the original string contains exactly n such ... 
- 关于SQL Cookbook里dept与emp表结构以及数据
			用MYSQL 写了一下,将number变成int, to_date去掉即可. DROP TABLE IF EXISTS `dept`; CREATE TABLE `dept` ( `DEPTNO` ) ... 
- LeetCode-Search in Rotated Sorted Array II
			Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ... 
- 堆排序(c++第一次尝试)
			对排序的实现思路有两种 第一种:1.构建最小堆.2.将最小堆的堆顶元素取出放到辅助数组的0号下标.3.重新调整成最小堆(向上调整) 4.重复2-3 第二种:1.构建最大堆.2.将堆顶元素(0号)与最后 ... 
