最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
安卓开发之Intent传递Object与List教程
时间:2015-06-20 编辑:简简单单 来源:一聚教程网
Intent 不仅可以传单个的值,也可以传对象与数据集合。 传递List
ArrayList
info.add(name);
info.add(website);
info.add(weibo);
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putStringArrayListExtra("infoList", info);
startActivity(intent);
接收String 的方法。
ArrayList
infoList = getIntent().getStringArrayListExtra("infoList");
传递Object,此Obiect对象需要实现Serializable接口,用Serializable方式传递Object的语法:bundle.putSerializable(key,object);用Serializable方式接收Object的语法:object=(Object) getIntent().getSerializableExtra(key);实现Serializable接口就是把对象序列化,然后再传输,和Java的常用编程没什么明显区别,而且Object不需要明显改变,推荐用这种方式。
下面是Object对象:
public class SerInfo implements Serializable {
private String name;
private String website;
private String weibo;
............
}
get,set方法自己创建,用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:
SerInfo serInfo = (SerInfo) getIntent().getSerializableExtra("serinfo");
这里讲的只是用Serializable这种方式传递数据,当然还有一种方式用Parcelable接口实现。
-
上一个: 爱思助手其他工具列表读取失败的解决办法
-
下一个: 解决iPhone手机充电慢的10个小技巧
相关文章
- sora软件价格介绍 02-22
- sora官网入口地址一览 02-22
- Sora生成的视频使用教程 02-22
- 《梦幻西游》元宵灯谜线索答案大全2024 02-22
- 《原神》有朋自远方来第一天通关攻略 02-22
- 《苏醒之路》四个结局达成攻略 02-22