作为android开发者大家都知道两个activity之间的跳转及数据的传递是通过intent和bundle来实现,在intent下有挺多方法来协助我们实现连个activity间的交互,但有时我们需要传递的不单单只是一个简单的数据类型,而是我们自己封转的数据对象,二进制对象,那我们改如何实现呢?

要实现它,我们有两个方法,都是去实现android里的接口,他们分别是serialiable和Parcelabel,对于serialable的实现方式比较简单,只需在我们的数据类实现它,并在activity下通过bundle的协助,使用putserialableExtras将对象存放在bundle中,而对于Parcelable我需要重新实现它下面的creator对象,之后通过intent进行传递。

下面是实现这两种方法的代码:

(1)Serialable

package cn.com.wd;import java.io.Serializable;public class Person implements Serializable{/** *  */private static final long serialVersionUID = 1L;private String name;private String age;private String sex;private String id;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getId() {return id;}public void setId(String id) {this.id = id;}}


package cn.com.wd;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;import android.widget.EditText;public class ObjectPassDemoActivity extends Activity {private EditText name,id,sex,age;private Button button;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        name = (EditText)findViewById(R.id.editname);        id = (EditText)findViewById(R.id.editage);        sex = (EditText)findViewById(R.id.editsex);        age = (EditText)findViewById(R.id.editage);        button = (Button)findViewById(R.id.next);        button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString namestr = name.getText().toString();String idstr = id.getText().toString();String sexstr = sex.getText().toString();String agestr = age.getText().toString();Person person = new Person();person.setAge(agestr);person.setId(idstr);person.setName(namestr);person.setSex(sexstr);Bundle bundle = new Bundle();bundle.putSerializable("personObject", person);Intent intent = new Intent(ObjectPassDemoActivity.this, ResultActivty.class);intent.putExtras(bundle);startActivity(intent);}});    }}


package cn.com.wd;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class ResultActivty extends Activity {    private TextView name,age,id,sex;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.result);name = (TextView)findViewById(R.id.name);age = (TextView)findViewById(R.id.age);id = (TextView)findViewById(R.id.id);sex = (TextView)findViewById(R.id.sex);    Intent intent = this.getIntent();    Bundle bundle = intent.getExtras();    Person person = (Person)bundle.getSerializable("personObject");    name.setText(person.getName());    age.setText(person.getAge());    id.setText(person.getId());    sex.setText(person.getSex());}      }


(2)Parcelable

package cn.com.PracelAbleTest;import java.util.HashMap;import android.os.Parcel;import android.os.Parcelable;public class Person implements Parcelable {public HashMap<String, Object> map = new HashMap<String, Object>();public String name;@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {// TODO Auto-generated method stubdest.writeMap(map);dest.writeString(name);}public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {@Overridepublic Person createFromParcel(Parcel source) {// TODO Auto-generated method stubPerson p = new Person();p.map = source.readHashMap(HashMap.class.getClassLoader());p.name = source.readString();return p;}@Overridepublic Person[] newArray(int size) {// TODO Auto-generated method stubreturn null;}};}


package cn.com.PracelAbleTest;import java.util.HashMap;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;public class ParcelableDemoActivity extends Activity {private Bitmap bitmap;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);        Intent intent = new Intent();        Person p = new Person();        p.map = new HashMap<String, Object>();        p.map.put("key","ido");        p.map.put("img", bitmap);        p.name = "ok";        intent.putExtra("key",p);        intent.setClass(ParcelableDemoActivity.this,NextDemo.class);        startActivity(intent);    }}


package cn.com.PracelAbleTest;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.widget.ImageView;public class NextDemo extends Activity{    ImageView iv ;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);    setContentView(R.layout.main);    iv = (ImageView)findViewById(R.id.image);    Intent i = getIntent();    Person p = i.getParcelableExtra("key");    System.out.println("----->"+p.name);    System.out.println("----->"+p.map.get("key"));    Bitmap bitmap = (Bitmap) p.map.get("img");    iv.setImageBitmap(bitmap);}}


更多相关文章

  1. Android菜鸟的成长笔记(22)——Android进程间传递复杂数据(AIDL)
  2. 【Android 内存优化】Bitmap 图像尺寸缩小 ( 考虑像素密度、针对
  3. win10通过网线连接树莓派和PC方法(Android things)
  4. [android盈利模式探索]我也分享一下我Android的收入数据
  5. Android root检测方法小结
  6. android之计时器(Chronometer)的使用以及常用的方法
  7. win7与android设备通过蓝牙串口的连接方法
  8. 分享一种最简单的Android打渠道包的方法

随机推荐

  1. Android(安卓)资源文件中@、@android:typ
  2. android中星级评分控件RatingBar的使用
  3. Android(安卓)相对布局属性全集
  4. Android中的主题Theme
  5. Android(安卓)TextView跑马灯效果
  6. Android(安卓)资源文件中@、@android:typ
  7. 相对布局相关属性
  8. Android课程大纲
  9. Android控件——TextView、EditText、Ima
  10. 02常用控件属性以及四种监听事件的写法