使用intent在Activity间传递数据时,好像都是一些基本类型的数据,如果要传递类对象的话,要用到Parcelable,Serializable。于是,学习了一下如何传递类对象,现把自己做的demo贴出来,请大家来指正。如果有更好的方法,望分享之。
1.创建主Activity,在里面实现button的click事件,并实现对象传递。
/**
* 対象伝送
*/
private void onButtonParcelableClick() {
// UserSpeak speak = new UserSpeak();
// speak.setLanguage("English");
//
// CustomeParcelable parcelable = new CustomeParcelable(speak);
UserSpeak speak = new UserSpeak();
speak.setLanguage("English");
CustomeParcelable parcelable = new CustomeParcelable();
parcelable.setInfo(speak);
parcelable.setName("Hanmeimei");
parcelable.setAge(20);
Intent intent = new Intent(this, ChildParcelableActivity.class);
intent.putExtra("app_parcelable", parcelable);
startActivity(intent);
}
main.xml定义如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="第二页"
android:id="@+id/buttonParcelable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
2,自定义类UserSpeak,声明implements接口
public class UserSpeak implements Serializable {
private static final long serialVersionUID = 1L;
private String language;
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
3.自定义类CustomeParcelable,声明Parcelable接口
public class CustomeParcelable implements Parcelable {
private UserSpeak speak;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public UserSpeak getInfo() {
return speak;
}
public void setInfo(UserSpeak info) {
this.speak = info;
}
public CustomeParcelable() {
}
public CustomeParcelable(Parcel source) {
this.speak = (UserSpeak) source.readValue(UserSpeak.class
.getClassLoader());
this.name = source.readString();
this.age = source.readInt();
}
// public CustomeParcelable(UserSpeak info) {
//
// this.speak = info;
// }
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(speak);
dest.writeString(name);
dest.writeInt(age);
}
public static final Parcelable.Creator<CustomeParcelable> CREATOR = new Parcelable.Creator<CustomeParcelable>() {
@Override
public CustomeParcelable createFromParcel(Parcel source) {
return new CustomeParcelable(source);
}
@Override
public CustomeParcelable[] newArray(int size) {
return new CustomeParcelable[size];
// throw new UnsupportedOperationException();
}
};
}
4.接受主Activity传过来的数据并显示
public class ChildParcelableActivity extends Activity {
private TextView textViewChildName;
private TextView textViewChildAge;
private TextView textViewLanguage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child);
textViewChildName = (TextView) this
.findViewById(R.id.textViewChildName);
textViewChildAge = (TextView) this.findViewById(R.id.textViewChildAge);
textViewLanguage = (TextView) this.findViewById(R.id.textViewLanguage);
CustomeParcelable p = getIntent().getParcelableExtra("app_parcelable");
UserSpeak info = p.getInfo();
textViewChildName.setText(p.getName());
textViewChildAge.setText(String.valueOf(p.getAge()));
textViewLanguage.setText("She can speak " + info.getLanguage());
}
}
5.效果