android 在activity之間如何傳遞復(fù)雜數(shù)據(jù)類(lèi)型
在Activity傳送復(fù)雜數(shù)據(jù)時(shí),(這里用ArrayList
首先建立兩個(gè)javabean,一個(gè)實(shí)現(xiàn)Serializable接口,一個(gè)實(shí)現(xiàn)Parcelable接口:
package?org.example.passdata; import?java.io.Serializable; public?class?Student?implements?Serializable?{ ????private?static?final?long?serialVersionUID?=?6130446011020753806L; ????private?String?id; ????private?String?name; ????public?Student(String?id,?String?name)?{ ????????this.id?=?id; ????????this.name?=?name; ????} ????public?String?getId()?{ ????????return?this.id; ????} ????public?void?setId(String?id)?{ ????????this.id?=?id; ????} ????public?String?getName()?{ ????????return?this.name; ????} ????public?void?setName(String?name)?{ ????????this.name?=?name; ????} ????@Override ????public?String?toString()?{ ????????return?"id:"?+?this.id?+?"?name:"?+?this.name; ????} }
package?org.example.passdata; import?android.os.Parcel; import?android.os.Parcelable; public?class?Teacher?implements?Parcelable?{ ????private?String?id; ????private?String?name; ????public?Teacher(String?id,?String?name)?{ ????????this.id?=?id; ????????this.name?=?name; ????} ????public?String?getId()?{ ????????return?this.id; ????} ????public?void?setId(String?id)?{ ????????this.id?=?id; ????} ????public?String?getName()?{ ????????return?this.name; ????} ????public?void?setName(String?name)?{ ????????this.name?=?name; ????} ????@Override ????public?int?describeContents()?{ ????????return?0; ????} ????@Override ????public?void?writeToParcel(Parcel?dest,?int?flags)?{ ????????dest.writeString(id); ????????dest.writeString(name); ????} ????@Override ????public?String?toString()?{ ????????return?"id:"?+?this.id?+?"?name:"?+?this.name; ????} ????public?static?final?Parcelable.CreatorCREATOR?=?new?Parcelable.Creator()?{ ????????@Override ????????public?Teacher?createFromParcel(Parcel?source)?{ ????????????Teacher?teacher?=?new?Teacher(source.readString(),?source.readString()); ????????????return?teacher; ????????} ????????@Override ????????public?Teacher[]?newArray(int?size)?{ ????????????return?null; ????????} ????}; }
然后在MainActrivity中發(fā)送數(shù)據(jù):
package?org.example.passdata; import?java.util.ArrayList; import?android.app.Activity; import?android.content.Intent; import?android.os.Bundle; import?android.view.View; import?android.view.View.OnClickListener; import?android.widget.Button; public?class?PassDataActivity?extends?Activity?implements?OnClickListener?{ ????private?Button?normalBtn,?serializableBtn,?parcelBtn; ????@Override ????public?void?onCreate(Bundle?savedInstanceState)?{ ????????super.onCreate(savedInstanceState); ????????setContentView(R.layout.main); ????????normalBtn?=?(Button)?findViewById(R.id.normalbtn); ????????serializableBtn?=?(Button)?findViewById(R.id.serializablebtn); ????????parcelBtn?=?(Button)?findViewById(R.id.parcelbtn); ????????normalBtn.setOnClickListener(this); ????????serializableBtn.setOnClickListener(this); ????????parcelBtn.setOnClickListener(this); ????} ????@Override ????public?void?onClick(View?v)?{ ????????if?(v.getId()?==?normalBtn.getId())?{ ????????????//?普通傳送 ????????}?else?if?(v.getId()?==?serializableBtn.getId())?{ ????????????//?Serializable序列化傳送 ????????????ArrayListlist?=?new?ArrayList(); ????????????Student?student?=?new?Student("001",?"naki"); ????????????list.add(student); ????????????Intent?intent?=?new?Intent(this,?SecondActivity.class); ????????????intent.putExtra("STUDENT_INFO",?list); ????????????startActivity(intent); ????????}?else?if?(v.getId()?==?parcelBtn.getId())?{ ????????????//?parcel序列化傳送 ????????????ArrayListlist?=?new?ArrayList(); ????????????Teacher?teacher1?=?new?Teacher("001",?"techaer1"); ????????????list.add(teacher1); ????????????Intent?intent?=?new?Intent(this,?SecondActivity.class); ????????????intent.putExtra("TEACHER_INFO",?list); ????????????startActivity(intent); ????????} ????} }
在接收的Activity中接收數(shù)據(jù):
package?org.example.passdata; import?java.util.ArrayList; import?android.app.Activity; import?android.content.Intent; import?android.os.Bundle; import?android.widget.TextView; public?class?SecondActivity?extends?Activity?{ ????@Override ????protected?void?onCreate(Bundle?savedInstanceState)?{ ????????super.onCreate(savedInstanceState); ????????Intent?intent?=?getIntent(); ????????//?serializable ????????//?ArrayListlist?=?(ArrayList)?intent.getSerializableExtra("STUDENT_INFO"); ????????//?TextView?textView?=?new?TextView(this); ????????//?textView.setText(list.get(0).toString()); ????????//?setContentView(textView); ????????//?parcelble ????????ArrayListlist?=?intent.getParcelableArrayListExtra("TEACHER_INFO"); ????????TextView?textView?=?new?TextView(this); ????????textView.setText(list.get(0).toString()); ????????setContentView(textView); ????} }