Android的intent之间复杂参数的传递
老师在《Lecture03_应用程序框架及关键组件_1》课上有介绍了Intent是Activity与Activity之间,Activity与Service之间传递参数的介质,并有使用Intent和Bundle在组件之间传递数据的用法和例子,而这两种通常实现的是Java基本对象类型和String的传递。
在实际项目中,页面之间传值,除了以上几种,经常还有传递Object对象、List类型、List<Object>类型和全局变量等等的需求。本文就是介绍怎么传递这几种类型的参数。
一、传递List<String>和List<Integer>
以下以传递List<String>为例,发送List<String>语法为:
intent.putStringArrayListExtra(key, list);
接收List<String>的语法为:
list = (ArrayList<String>)getIntent().getStringArrayListExtra(key);
以下是一个运用实例:
[java] <span style="font-size:16px;">// =============发送List<String>=============
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("string1");
stringList.add("string2");
stringList.add("string3");
Intent intent = new Intent();
intent.setClass(ListDemoActivity.this, StringListActivity.class);
intent.putStringArrayListExtra("ListString", stringList);
startActivity(intent);
// ====================接收List<String>======================
ArrayList<String> stringList = (ArrayList<String>) getIntent().getStringArrayListExtra("ListString");
</span>
<span style="font-size:16px;">// =============发送List<String>=============
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("string1");
stringList.add("string2");
stringList.add("string3");
Intent intent = new Intent();
intent.setClass(ListDemoActivity.this, StringListActivity.class);
intent.putStringArrayListExtra("ListString", stringList);
startActivity(intent);
// ====================接收List<String>======================
ArrayList<String> stringList = (ArrayList<String>) getIntent().getStringArrayListExtra("ListString");
</span>
List<Integer>类似以上的操作调用下面的方法也可以实现发送和接收:
intent.putIntegerArrayListExtra(key, list);
list =(ArrayList<Integer>) getIntent().getIntegerArrayListExtra(key);
一、使用Serializable和Parcelable两种方式传递Object
Android的Intent之间传递对象有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key,Object)。方法中的Object要满足一定的条件,前者实现了Serializable接口,而后者实现了Parcelable接口。
以下是实现了Serializable接口的User类,命名为SerializableUser纯粹是从类名方便区分实现了Parcelable接口的User类,实际开发中不建议这么命名:
[java] <span style="font-size:16px;">public class SerializableUser implements Serializable {
private String userName;
private String password;
public SerializableUser() {
}
public SerializableUser(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
</span>
<span style="font-size:16px;">public class SerializableUser implements Serializable {
private String userName;
private String password;
public SerializableUser() {
}
public SerializableUser(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
</span>
以下是实现了Parcelable接口的User类:
[java] <span style="font-size:16px;">public class ParcelableUser implements Parcelable {
private String userName;
private String password;
public ParcelableUser() {
}
public ParcelableUser(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static final Parcelable.Creator<ParcelableUser> CREATOR = new Creator<ParcelableUser>() {
@Override
public ParcelableUser createFromParcel(Parcel source) {
ParcelableUser parcelableUser = new ParcelableUser();
parcelableUser.userName = source.readString();
parcelableUser.password = source.readString();
return parcelableUser;
}
@Override
public ParcelableUser[] newArray(int size) {
return new ParcelableUser[size];
}
};
@Override
public int describeContents() {
&nbs
补充:移动开发 , Android ,