Android intent传递list或对象
方法一:
如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用
Java代码
intent.putStringArrayListExtra(name, value)
intent.putIntegerArrayListExtra(name, value)
方法二:
如果传递的是List<Object>,可以把list强转成Serializable类型,然后通过
Java代码 putExtras(key, (Serializable)list)
方法传递过去,接受的时候用
Java代码 (List<YourObject>) getIntent().getSerializable(key)
就可以接受到List<YourObject>数据了
但是 切记 你的YourObject类必须要实现Serializable接口
方法三:
一种是
Java代码 Bundle.putSerializable(Key,Object);
另一种是
Java代码 Bundle.putParcelable(Key, Object);
当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口
方法四:
用intent传来传去 觉得不方便 我们可以写一个在application里面的全局数据
1、创建一个属于你自己的android.app.Application的子类
2、在manifest中申明一下这个类,
3、这时android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。
继承Application
Java代码
class MyApp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
关于AndroidManifest.xml中的配置,原来直接给application加个name就可以了,如下面所示:
Java代码 <application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name">
使用
Java代码
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
***此为转载,不过感觉自己比较常用利于参开放在这里:http://hi.baidu.com/ihsauqaxblbdmwq/item/dfc9cf9c352b0bdf1a49dfd5
Intent intent = new Intent(this, ThirdActivity.class);
Person p = new Person();
p.setId(1);
p.setName("驰哥");
p.setSalary(456.1);
Person p2 = new Person();
p2.setId(2);
p2.setName("魏神");
p2.setSalary(111.111); List<Person> list = new ArrayList<Person>();
list.add(p);
list.add(p2);
Bundle extras = new Bundle();
extras.putSerializable("list", (Serializable) list);
intent.putExtras(extras);
startActivity(intent);
Bundle ex = intent.getExtras();
List<Person> p = (List<Person>) ex.getSerializable("list");
Toast.makeText(this, p.toString(), Toast.LENGTH_SHORT).show();
Android intent传递list或对象的更多相关文章
- android intent 传递list或者对象
(转:http://www.cnblogs.com/lee0oo0/archive/2012/09/24/2699805.html) 方法一: 如果单纯的传递List<String> 或者 ...
- Android Intent传递数据
刚开始看郭大神的<>,实现以下里面的一些例子.Intent传递数据. 我们利用显示的方式进行Intent的启动. 1.启动intent并输入数据. Intent intent=new In ...
- Android Intent传递对象小结
效果: 想看实例的,感兴趣的能够看之前的一篇文章 Android ViewPager Fragment实现选项卡 部分关键代码: public class SerializeActivity exte ...
- Android Intent传递对象摘要
效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG9uZ3NoZW5ncGVuZw==/font/5a6L5L2T/fontsize/400/fil ...
- Android Intent 基本使用及对象构成
Intent基本使用 Intent可以理解为不同组件通信的媒介或者信使. Intent可以启动一个Activity,也可以启动一个Service,还可以发起一个广播Broadcast. 具体方法如下表 ...
- Android:Intent传递数据的几种类型和源码实现
public class Intent implements Parcelable, Cloneable { //... private String mAction; private Uri m ...
- android activity传递实体类对象
通过实现Parcelable接口序列化对象的步骤: 1.实现Parcelable接口.2.并且实现Parcelable接口的public void writeToParcel(Parcel dest, ...
- Android Intent 传递数据注意事项
不要通过 Intent 在 Android 基础组件之间传递大数据(binder transaction缓存为 1MB),可能导致 OOM.
- android intent 传递 二进制数据
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 实现序列化.
随机推荐
- TopCoder SRM 633 Div.2 500 Jumping
题意:给一个点(x,y),给一些步长delta1,delta2...deltaN,问从(0,0)严格按照步长走完N步后能否正好到达(x,y)点. 解法:其实就是判断这些线段和(0,0)-(x,y)这条 ...
- 第13章 Windows内存体系结构
13.1 Windows的虚拟地址空间安排 13.1.1虚拟地址空间的分区(即虚拟地址空间布局) 进程的地址空间划分 分区 x86 32位 Windows 3GB用户模式下的x86 32位Window ...
- Params 方法参数
params,ref,out 方法参数 示例 在下面的方法使用中 OpenWindow(params object[] args) 传递的参数args添加了params修饰 public void O ...
- Cookie测试工具小汇
现在很多网站都用到Cookies,特别是用户的登陆以及购物网站的购物车. Cookies 通常用来存储用户信息和用户在某应用系统的操作,当一个用户使用Cookies 访问了某一个应用系统时,Web 服 ...
- 安装StarUML 及使用时序图(Sequence Diagram)和用例图(use case diagram)
时序图 用例图
- PHP安装memcache扩展接口步骤
1.将php_memcache.dll文件保存到php的应用程序扩展ext目录中 2.在php.ini配置文件添加扩展的位置,加入一行extension=php_memcache.dll 3.重新启动 ...
- Netty关闭客户端
在启动客户端的时候,我们一般会 channelFuture.channel().closeFuture().sync(); 这是一段阻塞的代码,除非链路断了,否则是不会终止的,我们可以在handler ...
- easyui-combobox的取值问题
例子:<select id="cc" class="easyui-combobox" name="cc" style="wi ...
- 深入分析 Javascript 单线程
面试的时候发现99%的童鞋不理解为什么JavaScript是单线程的却能让AJAX异步发送和回调请求,还有setTimeout也看起来像是多线程的?还有non-blocking IO, event l ...
- 如何清洗 Git Repo 代码仓库
git prune 如何清洗 Git Repo 代码仓库 在腾讯云上创建您的SQL Cluster>>> » 相信不少团队的代码仓库 Git Repo 变得越来越大. ...