Activity等相关各组件之间通过Intent传递值,支持基本数据类型和String对象及它们的数组对象byte、byte[]、char、char[]、boolean、boolean[]、short、short[]、int、int[]、long、long[]、float、float[]、double、double[]、String、String[],还有实现Serializable、Parcelable接口的类对象。
下面介绍如何在他们之间通过Intent传递类对象:
第一种:通过实现Serializable接口传递对象。
自定义的类:
public class CustomeClass implements Serializable{ /** * */ private static final long serialVersionUID = -7060210544600464481L; private String name; private String id; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
|
发送示例代码:
CustomeClass cc = new CustomeClass(); cc.setAge(21); cc.setId("123456"); cc.setName("mingkg21"); cc.setSex("男"); Intent intent = new Intent(this, PersonInfo.class); intent.putExtra("PERSON_INFO", cc); startActivity(intent);
|
接收示例代码:
Intent intent = getIntent(); CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO"); setTextView(R.id.id, cc.getId()); setTextView(R.id.name, cc.getName()); setTextView(R.id.sex, cc.getSex()); setTextView(R.id.age, String.valueOf(cc.getAge()));
|
第二种:通过实现Parcelable接口传递对象。
自定义类:
public class CustomeParcelable implements Parcelable { private String name; private String id; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public static final Parcelable.Creator<CustomeParcelable> CREATOR = new Creator<CustomeParcelable>(){ public CustomeParcelable createFromParcel(Parcel source) { // TODO Auto-generated method stub
CustomeParcelable cus = new CustomeParcelable(); cus.name = source.readString(); cus.id = source.readString(); cus.age = source.readInt(); cus.sex = source.readString(); return cus; } public CustomeParcelable[] newArray(int size) { // TODO Auto-generated method stub
return new CustomeParcelable[size]; } }; public int describeContents() { // TODO Auto-generated method stub
return 0; } public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub
dest.writeString(name); dest.writeString(id); dest.writeInt(age); dest.writeString(sex); } }
|
发送示例代码:
CustomeParcelable cc = new CustomeParcelable(); cc.setAge(21); cc.setId("123456"); cc.setName("mingkg21"); cc.setSex("男"); Intent intent = new Intent(this, PersonInfo.class); intent.putExtra("PERSON_INFO", cc); startActivity(intent);
|
接收示例代码:
Intent intent = getIntent(); CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO"); setTextView(R.id.id, cc.getId()); setTextView(R.id.name, cc.getName()); setTextView(R.id.sex, cc.getSex()); setTextView(R.id.age, String.valueOf(cc.getAge()));
|
参考网址:
http://mingkg21.javaeye.com/blog/438913
http://mingkg21.javaeye.com/blog/463895
阅读(3215) | 评论(0) | 转发(0) |