在很多时候,我们需要在Activity之间传递对象,比如当你点击了某列表的item,需要传递给下一个Activity该对象,那我们需要该怎么做呢?
Android支持两种传递对象的方式,一种是bundle.putSerializable方式,一种是bundle.putParcelable。
那么下面我们就用一个例子来实践Activity传递对象:
1.首先建立两个类,一个Teacher类表示老师,一个Student类表示学生。内容分别如下:
<span style="font-size:18px;">public class Teacher implements Serializable { private static final long serialVersionUID = -7060210544600464481L; private String name; private int age; private String addr; public Teacher(String name, int age, String addr) { super(); this.name = name; this.age = age; this.addr = addr; } 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 String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { String temp = "老师姓名:" + this.name + " 年龄:" + this.age + " 地址:" + this.addr; return temp; } }</span>
<span style="font-size:18px;">import android.os.Parcel; import android.os.Parcelable; public class Student implements Parcelable { private String name; private int age; private String addr; public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() { @Override public Student[] newArray(int size) { return new Student[size]; } @Override public Student createFromParcel(Parcel source) { Student stu = new Student(); stu.name = source.readString(); stu.age = source.readInt(); stu.addr = source.readString(); return stu; } }; @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); dest.writeString(addr); } public Student(){ } public Student(String name, int age, String addr) { super(); this.name = name; this.age = age; this.addr = addr; } 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 String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { String temp = "学生姓名:" + this.name + " 年龄:" + this.age + " 地址:" + this.addr; return temp; } }</span>
其中Teacher类实现了Serializable接口,Student类实现了Parcelable接口。
2.创建两个Activity,第一个Activity叫做FirstActivity,它里面有两个对象:一个Teacher对象,一个Student对象,我们现在需要将这两个对象传递给第二个Activity,即SecondActivity。
首先我们看一下FirstActivity的布局:
FirstActivity的内容如下:
import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class FirstActivity extends Activity implements OnClickListener{ public static final String TEACHER_KEY = "key_teacher"; public static final String STUDENT_KEY = "key_student"; private Teacher teacher = new Teacher("刘备", 36, "荆州"); private Student student = new Student("张飞", 32, "新野"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); findViewById(R.id.button1).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Bundle bundle = new Bundle(); bundle.putSerializable(TEACHER_KEY, teacher); bundle.putParcelable(STUDENT_KEY, student); Intent intent = new Intent(FirstActivity.this,SecondActivity.class); intent.putExtras(bundle); startActivity(intent); break; default: break; } } }
里面就只有一个TextView。
然后我们看一下SecondActivity的内容,它主要是将传递过来的对象显示到TextView当中:
import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.widget.TextView; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); Teacher t = (Teacher) bundle.getSerializable(FirstActivity.TEACHER_KEY); Student s = bundle.getParcelable(FirstActivity.STUDENT_KEY); String text = t.toString() + "\n" + s.toString(); ((TextView)findViewById(R.id.text_view)).setText(text); } }
很显然,这两种方式都可以成功的在Activity之间传递对象。
希望以上内容对各位读者能够带来帮助!
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/bear_huangzhen/article/details/47059813