读书人

Intent传接对象的两种实现

发布时间: 2013-11-05 14:40:42 作者: rapoo

Intent传递对象的两种实现

MianActivity.java


public class IntentDemo extends Activity implements OnClickListener{
??? /** Called when the activity is first created. */
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? Button btn_01 = (Button) findViewById(R.id.btn_01);
??????? Button btn_02 = (Button) findViewById(R.id.btn_02);
???????
??? }

?@Override
?public void onClick(View v) {
? // TODO Auto-generated method stub
? switch(v.getId()){
? case R.id.btn_01:{
?
?? Intent intent = new Intent(this,PersonView.class);
?? Person mPerson = new Person();
?? mPerson.setAge(20);
?? mPerson.setName("moon");
?? Bundle bundle = new Bundle();
?? bundle.putSerializable("person", mPerson);
?? intent.putExtras(bundle);
?? startActivity(intent);
?? break;
? }
??
? case R.id.btn_02:{
?? Intent intent = new Intent(this,BookView.class);
?? Book book = new Book();
?? book.setName("manmonth");
?? book.setTime("1975");
?? book.setAuthor("Brooks");
?? Bundle bundle = new Bundle();
?? bundle.putParcelable("book", book);
?? intent.putExtras(bundle);
?? startActivity(intent);
?? break;
? }
??
? }
?}
}

Book.java:


public class Book implements Parcelable{

?private String name;
?private String author;
?private String time;

?public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>(){

? @Override
? public Book createFromParcel(Parcel source) {
?? // TODO Auto-generated method stub
?? Book mBook = new Book();
?? mBook.name?? = source.readString();
?? mBook.time?? = source.readString();
?? mBook.author = source.readString();
?? return mBook;
? }

? @Override
? public Book[] newArray(int size) {
?? // TODO Auto-generated method stub
?? return new Book[size];
? }
?
?};
?
?@Override
?public int describeContents() {
? // TODO Auto-generated method stub
? return 0;
?}

?@Override
?public void writeToParcel(Parcel dest, int flags) {
? // TODO Auto-generated method stub
? dest.writeString(name);
? dest.writeString(author);
? dest.writeString(time);
?}

?public String getName() {
? return name;
?}

?public void setName(String name) {
? this.name = name;
?}

?public String getTime() {
? return time;
?}

?public void setTime(String time) {
? this.time = time;
?}

?public String getAuthor() {
? return author;
?}

?public void setAuthor(String author) {
? this.author = author;
?}
?
}
?

Person.java:

public class Person implements Serializable{
?
?private static final long serialVersionUID = 1L;
?
?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 class PersonView extends Activity {

?@Override
?protected void onCreate(Bundle savedInstanceState) {
? // TODO Auto-generated method stub
? super.onCreate(savedInstanceState);
? TextView text = new TextView(this);
? Person person = (Person)getIntent().getSerializableExtra("person");
? text.setText("name:"+person.getName()+"/nage:"+person.getAge()+"/n");
? setContentView(text);
?}
}
?

BookView:

package cn.edu.wtu;


public class BookView extends Activity {

?@Override
?protected void onCreate(Bundle savedInstanceState) {
? // TODO Auto-generated method stub
? super.onCreate(savedInstanceState);
? TextView text = new TextView(this);
? Book book = (Book) getIntent().getParcelableExtra("book");
? text.setText("name:"+book.getName()+"/nautor:"+book.getAuthor()+"/ntime:"+book.getTime());
? setContentView(text);
?}

}

?

出自http://blog.csdn.net/fuuckwtu/article/details/6522098

读书人网 >移动开发

热点推荐