来源开发界 http://www.kaifajie.cn
原文:http://wenku.baidu.com/view/d1250a16cc7931b765ce1566.html
在开发中,本人遇到这样问题想在两个activity之间传递某个自定义类对象,但是使用Bundle发现里面只能传送已经封装好的int,float,String等类型,自定义的类无法发送。
后来上网查阅相关资料发现Bundle中有这两个方法putSerializable和putLongArray两个方法对自定义数据进行序列化即可实现所希望的功能。
需要注意的是你所传递的自定义数据必须implementsSerializable或者Parcelable接口,可以用bundleputSerializable(String,Serizlizable)数据或者直接用intentputExtrr(String,Serizlizable)数据。如果该自定义数据里面嵌套了其他自定义数据类型,则必须都要implementsSerializable或者Parcelable接口才行,不然会报错...
==============================================================================
原文:http://blog.csdn.net/snowleopard_wu/article/details/7035994
作为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;
-
- @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() {
- @Override
- public void onClick(View v) {
-
- String 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;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.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;
-
- @Override
- public int describeContents() {
-
- return 0;
- }
-
- @Override
- public void writeToParcel(Parcel dest, int flags) {
-
- dest.writeMap(map);
- dest.writeString(name);
- }
-
- public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
-
- @Override
- public Person createFromParcel(Parcel source) {
-
- Person p = new Person();
- p.map = source.readHashMap(HashMap.class.getClassLoader());
- p.name = source.readString();
- return p;
- }
-
- @Override
- public Person[] newArray(int size) {
-
- return 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;
-
- @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 ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.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);
- }
-