Intent.putExtra()传递Object对象或者ArrayList<Object> (转)
Intent传递基本类型相信大家都十分熟悉,如何传递Object对象或者ArrayList<Object>对象呢?
可以通过:
(1)public Intent putExtra (String name, Serializable value)
(2)public Intent putExtra (String name, Parcelable value)
public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
一、通过实现Serializable接口传递
(1)首先让Person继承Serializable接口
- package com.example.hellojni;
- import java.io.Serializable;
- public class Person implements Serializable{
- /**
- * Auto generate
- */
- private static final long serialVersionUID = -5053412967314724078L;
- private String name;
- private int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
- package com.example.hellojni;
- import java.io.Serializable;
- public class Person implements Serializable{
- /**
- * Auto generate
- */
- private static final long serialVersionUID = -5053412967314724078L;
- private String name;
- private int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
(2)通过Intent来进行传输,具体方法是:public Intent putExtra (String name, Serializable value)
- Intent intent = new Intent(this, OtherActivity.class);
- intent.putExtra("person", new Person("bear", 22));
- startActivity(intent);
- Intent intent = new Intent(this, OtherActivity.class);
- intent.putExtra("person", new Person("bear", 22));
- startActivity(intent);
(3)另外一个Activity中打印值:
- Intent intent = getIntent();
- Person person = (Person)intent.getSerializableExtra("person");
- System.out.println("name:" + person.getName());
- System.out.println("age:" + person.getAge());
- Intent intent = getIntent();
- Person person = (Person)intent.getSerializableExtra("person");
- System.out.println("name:" + person.getName());
- System.out.println("age:" + person.getAge());
(4)传递ArrayList<Person>:
- Intent intent = new Intent(this, OtherActivity.class);
- ArrayList<Person> personList= new ArrayList<Person>();
- initList(personList); //初始化personList
- intent.putExtra("personList", personList);
- startActivity(intent);
- Intent intent = new Intent(this, OtherActivity.class);
- ArrayList<Person> personList= new ArrayList<Person>();
- initList(personList); //初始化personList
- intent.putExtra("personList", personList);
- startActivity(intent);
(注意传值类型必须是ArrayList而不能是List。调用的还是public Intent putExtra (String name, Serializable value)这个函数,另外一边获取的时候强制转化为ArrayList<Person>即可)
二、通过实现Parcelable接口传递
同上,Person类实现Parcelable接口即可:
传递Object用:public Intent putExtra (String name, Parcelable value)
传递ArrayList<Object>用:public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
(同是序列化的接口,为神马Parcelable分开提供了2个函数,而传Serializable只提供了一个?这里我也很不解。。。)
转自:链接
Intent.putExtra()传递Object对象或者ArrayList<Object> (转)的更多相关文章
- Object类 任何类都是object类的子类 用object对象接收数组 object类的向上向下转型
任何类都是object类的子类 用object对象接收数组 object类的向上向下转型
- Scala学习教程笔记二之函数式编程、Object对象、伴生对象、继承、Trait、
1:Scala之函数式编程学习笔记: :Scala函数式编程学习: 1.1:Scala定义一个简单的类,包含field以及方法,创建类的对象,并且调用其方法: class User { private ...
- javascript ES5 Object对象
原文:http://javascript.ruanyifeng.com/stdlib/object.html 目录 概述 Object对象的方法 Object() Object.keys(),Obje ...
- Using QueryRunner to insert ArrayList<Object[]>
使用QueryRunner 结合c3p0进行数据库操作时候, 需求:list<bean>进行插入数据库中,但是QueryRunner 仅仅支持batch():批处理: Object[][] ...
- (80)Wangdao.com第十六天_JavaScript Object 对象的相关方法
Object 对象的相关方法 Object.getPrototypeOf() 返回参数对象的原型. 这是获取某对象的原型对象的标准方法. var F = function () {}; var f = ...
- .NET:不要使用扩展方法扩展Object对象。
C#的扩展方法算是一种Minin(掺入)机制,掺入方法有其合理的使用场景,这里说说一种不好的使用场景(个人意见):不要使用扩展方法扩展Object对象.扩展Object会对所有类型的示例有侵入,特别是 ...
- javascript——对象的概念——Object(未完)
http://www.blogjava.net/zkjbeyond/archive/2006/04/16/41336.html javascript中对象只包括属性和方法两种成员.ECMA-262 把 ...
- List<List<Object>> list = new ArrayList<List<Object>>(); 求回答补充问题 list.get(position).add(Object);为什么会报错啊我想在对应的list里面添加对象
public static void main(String[] args){ List<List<Object>> list = new ArrayList<List& ...
- Android 通过 Intent 传递类对象或list对象
(转:http://www.cnblogs.com/shaocm/archive/2013/01/08/2851248.html) Android中Intent传递类对象提供了两种方式一种是 通过实现 ...
随机推荐
- 如何使用shell脚本快速排序和去重文件数据
前面写过一篇通过shell脚本去重10G数据的文章,见<用几条shell命令快速去重10G数据>.然而今天又碰到另外一个业务,业务复杂度比上次的单纯去重要复杂很多.找了很久没有找到相应的办 ...
- spring3 的restful API RequestMapping介绍
原文链接:http://www.javaarch.net/jiagoushi/694.htm spring3 的restful API RequestMapping介绍 在spring mvc中 @R ...
- Java多线程Socket在控制台输出的多人聊天室编程
服务器端代码 import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java ...
- [Android]如何获取当前用户设置的时区
方法:TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);获取的值如GMT+08:00,GMT-04:00,EDT 另附:国家码查询 ...
- C#字符串的四舍五入
Round(Decimal) Round(Double) Round(Decimal, Int32) Round(Decimal, MidpointRounding) Round(Double, In ...
- code vs1506传话(塔尖)+tarjan图文详解
1506 传话 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 一个朋友网络,如果a认识b,那么如果a第一次收到 ...
- [Android] 如何查看apk需要支持的Android版本
reference to : http://blog.csdn.net/huiguixian/article/details/39928089 如果有一个apk,需要知道他最低安装支持的Android ...
- Redis内存管理(二)
上一遍详细的写明了Redis为内存管理所做的初始化工作,这篇文章写具体的函数实现. 1.zmalloc_size,返回内存池大小函数,因为库不同,所以这个函数在内部有很多的宏定义,通过具体使用的库来确 ...
- 模拟赛1103d1
取模(mod) [题目描述] 有一个整数a和n个整数b_1, -, b_n.在这些数中选出若干个数并重新排列,得到c_1,-, c_r.我们想保证a mod c_1 mod c_2 mod - mod ...
- 在R语言中无法设置CRAN镜像问题
很大的可能是因为使用的浏览器不是IE浏览器的问题,因为CRAN的镜像需要用IE浏览器来打开. 只需要按照下面设置即可: 1.打开IE-->设置-->Internet选项-->高级 2 ...