(1)、Person对象核心代码:
(2)、MainActivity页面:
public class Person implements Serializable {
private String userName;
private int age;
private int sex;
private List<String> infoList;
public List<String> getInfoList() {
return infoList;
}
public void setInfoList(List<String> infoList) {
this.infoList = infoList;
}
public String getUserName() {
return userName;
}
publicvoid setUserName(String userName) {
this.userName = userName;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age = age;
}
publicint getSex() {
returnsex;
}
publicvoid setSex(int
this.sex = sex;
}
}
(3)、MainActivity页面:
publicclass MainActivity extends Activity {
private Person person = null;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
person = new Person();
person.setUserName("wangxiangjun");
person.setAge(40);
person.setSex(1);
person.setInfoList(list);
}
public void clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_submit:
Intent intent = new Intent(MainActivity.this,
ReceiverActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("person", person);
intent.putExtras(bundle);
startActivity(intent);
break;
}
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}
publicclass ReceiverActivity extends Activity {
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
// 接收Serializable对象
Person data = (Person) bundle.getSerializable("person");
setTitle(data.getUserName() + ":" + data.getAge() + ":"
+ data.getInfoList());
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.receiver, menu);
returntrue;
}
}