原文连接:http://blog.csdn.net/Android_Tutor/archive/2010/07/16/5740845.aspx
使用intent可以在两个Acitivity之间传递数据,可以是int,string 数组,list等等。
但是有时候要传递一个对象,那怎么办呢
网上找了资料,并且运行了一下
mian.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:id="@+id/btn_01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Serilizable"/>
<Button android:id="@+id/btn_02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Parcelable"/>
</LinearLayout>
MianActivity.java
package cn.edu.wtu;
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 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);
btn_01.setOnClickListener(this);
btn_02.setOnClickListener(this);
}
@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:
package cn.edu.wtu;
import android.os.Parcel;
import android.os.Parcelable;
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:
package cn.edu.wtu;
import java.io.Serializable;
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;
}
}
PersonView:
package cn.edu.wtu;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
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;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
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);
}
}