一、传递List
传递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接口
用Serializable方式传递Object的语法:bundle.putSerializable(key,object);
用Serializable方式接收Object的语法:object=(Object) getIntent().getSerializableExtra(key);
实现Serializable接口就是把对象序列化,然后再传输,和Java的常用编程没什么明显区别,而且Object不需要明显改变,推荐用这种方式。
Object实现Serializable
- package com.wirelessqa.testintent;
-
- import java.io.Serializable;
-
-
-
-
-
- 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
-
- SerInfo serInfo = (SerInfo) getIntent().getSerializableExtra("serinfo");
2.2 使用Parcelable方式
前提:Object需要实现Parcelable接口
用Parcelable方式传递Object的语法:bundle.putParcelable(key,object);
用Parcelable方式接收Object的语法:object=(Object) getIntent().getParcelableExtra(key);
实现Parcelable接口的类比较复杂,Parcelable是个什么东西呢?
Android提供了一种新的类型:Parcel,被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以外,只有实现了Parcelable接口的类才能被放入Parcel中。
实现Parcelable接口需要实现三个方法: 1)writeToParcel方法。该方法将类的数据写入外部提供的Parcel中。
声明:writeToParcel(Parcel dest, int flags)。
2)describeContents方法。直接返回0就可以。
3)静态的Parcelable.Creator<T>接口,本接口有两个方法:createFromParcel(Parcel in) 实现从in中创建出类的实例的功能。
newArray(int size) 创建一个类型为T,长度为size的数组, returnnew T[size];即可。本方法是供外部类反序列化本类数组使用。
Object需要实现Parcelable接口
- package com.wirelessqa.testintent;
-
- import android.os.Parcel;
- import android.os.Parcelable;
-
-
-
-
-
- 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() {
-
- return 0;
- }
-
- @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>() {
-
- @Override
- public ParInfo createFromParcel(Parcel source) {
- ParInfo parInfo = new ParInfo();
- parInfo.name = source.readString();
- parInfo.website= source.readString();
- parInfo.weibo = source.readString();
- return parInfo;
- }
-
- @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);
- intent.setClass(MainActivity.this, ResultActivity.class);
- intent.putExtras(bundle);
- startActivity(intent);
用Parcelable方式接收Object
-
- ParInfo parInfo = (ParInfo) getIntent().getParcelableExtra("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>的方法
-
-
- ArrayList<SerInfo> listObj = (ArrayList<SerInfo>) getIntent().getSerializableExtra("listobj");
-
- <span style="font-size:24px; color:#ff0000">源码下载:http: