Activity间要传递数据,很多时候简单的int和string满足不了需求,我们在面向对象中已经习惯了类和对象,
那么下面就说一下如何传递类对象。
一、单个对象
Android中有两种办法来完成这件事。一个是Java的序列化(Serializable),另一个是Android的Parcelable序列化方法。
对于第一种办法实现很简单,只需要实现Serializable接口就好了。就像下面是一个person类:
package com.linc.meta; import java.io.Serializable; @SuppressWarnings("serial") public class Person2 implements Serializable { private String Name = null; private String Gender = null; private int HP = 0; private String Summary = null; private String Skill = null; public Person2(String name,String gender,int HP,String summary,String skill) { this.Name = name; this.Gender = gender; this.Summary = summary; this.HP = HP; this.Skill = skill; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getGender() { return Gender; } public void setGender(String gender) { Gender = gender; } public int getHP() { return HP; } public void setHP(int hP) { HP = hP; } public String getSummary() { return Summary; } public void setSummary(String summary) { Summary = summary; } public String getSkill() { return Skill; } public void setSkill(String skill) { Skill = skill; } }
怎么样,很简单的把原来的person类实现Serializable接口就可以了。
对于第二种办法,实现起来还是稍微有点小磕绊。这是Android新提供的序列化类,首先要看看Parcel类 ,它是一个数据的容器,只要你的类实现Parcelable接口,并完成几个步骤就可以放到Parcel中传递了。
有三个步骤要完成:
一、完成writeToParcel(Parcel dest, int flags)方法:该方法将类的数据写入外部提供的Parcel中
二、完成describeContents():这是返回描述性的东西,返回值是整型,通常是0.如果知道marshalled,请看CONTENTS_FILE_DESCRIPTOR
三、Parcelable.Creator 接口
请看下面,还是person类,它实现了Parcelable接口。
package com.linc.meta; import android.os.Parcel; import android.os.Parcelable; public class Person implements Parcelable { private String Name = null; private String Gender = null; private int HP = 0; private String Summary = null; private String Skill = null; public Person(String name,String gender,int HP,String summary,String skill) { this.Name = name; this.Gender = gender; this.Summary = summary; this.HP = HP; this.Skill = skill; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getGender() { return Gender; } public void setGender(String gender) { Gender = gender; } public int getHP() { return HP; } public void setHP(int hP) { HP = hP; } public String getSummary() { return Summary; } public void setSummary(String summary) { Summary = summary; } public String getSkill() { return Skill; } public void setSkill(String skill) { Skill = skill; } //Describe the kinds of special objects contained in this Parcelable's marshalled representation. //CONTENTS_FILE_DESCRIPTOR @Override public int describeContents() { // TODO Auto-generated method stub return 0; } //该方法将类的数据写入外部提供的Parcel中 @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(Name); dest.writeString(Gender); dest.writeString(Summary); dest.writeInt(HP); dest.writeString(Skill); } public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { @Override public Person createFromParcel(Parcel source) { return new Person(source); } @Override public Person[] newArray(int size) { return new Person[size]; } }; private Person(Parcel dest) { this.Name = dest.readString(); this.Gender = dest.readString(); this.Summary = dest.readString(); this.HP = dest.readInt(); this.Skill = dest.readString(); } }
activity A
package com.linc; import com.linc.meta.Person; import com.linc.meta.Person2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class ActivityA extends Activity { private TextView txt ; private Button btn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt = (TextView)findViewById(R.id.txt); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { transferData(); } }); Intent intent = getIntent(); Person person = (Person) intent.getParcelableExtra("person"); if(person != null) txt.setText("person :\n " + person.getName() + " \n " + person.getGender() + " \n " + person.getSummary() + " \n " + person.getHP() + " \n " + person.getSkill()); } private void transferData() { Person2 person = new Person2("张飞", "男", 4, "五虎上将之一。燕山人士,喜欢喝酒,生性豪放重情义。", "咆哮"); Intent intent = new Intent(); intent.setClass(this, ActivityB.class); intent.putExtra("person2", person); startActivity(intent); } }
package com.linc; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.linc.meta.Person; import com.linc.meta.Person2; public class ActivityB extends Activity { private TextView txt ; private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt = (TextView)findViewById(R.id.txt); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { transferData(); } }); Intent intent = getIntent(); if(intent != null) { Person2 person = (Person2) intent.getSerializableExtra("person2"); txt.setText("person : \n" + person.getName() + " \n " + person.getGender() + " \n " + person.getSummary() + " \n " + person.getHP() + " \n " + person.getSkill()); } } private void transferData() { Person person = new Person("黄盖", "男", 4, "东吴老将,配合周瑜使用苦肉计,骗过了将干,舍身无畏。", "苦肉"); Intent intent = new Intent(); intent.setClass(this, ActivityA.class); intent.putExtra("person", person); startActivity(intent); } }
二、List
也可以用Serialize和parcelable来传递多个类对象,这时要强调的是用ArrayList。就是把多个person放到list中,然后取时用intent.getParcelableArrayListExtra("person_list")取出。就okay了。
序列化也一样,发送正常发,取时类似这样:data = (ArrayList<Person>)getIntent().getSerializableExtra("person_list");
源码在这里。