[android]Android怎样能有两个Activity传递的自定义数据

来源开发界 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

          

[java]  view plain copy
  1. package cn.com.wd;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Person implements Serializable{  
  6.     /** 
  7.      *  
  8.      */  
  9.     private static final long serialVersionUID = 1L;  
  10.     private String name;  
  11.     private String age;  
  12.     private String sex;  
  13.     private String id;  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public String getAge() {  
  21.         return age;  
  22.     }  
  23.     public void setAge(String age) {  
  24.         this.age = age;  
  25.     }  
  26.     public String getSex() {  
  27.         return sex;  
  28.     }  
  29.     public void setSex(String sex) {  
  30.         this.sex = sex;  
  31.     }  
  32.     public String getId() {  
  33.         return id;  
  34.     }  
  35.     public void setId(String id) {  
  36.         this.id = id;  
  37.     }  
  38.   
  39. }  


 

[java]  view plain copy
  1. package cn.com.wd;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.   
  11. public class ObjectPassDemoActivity extends Activity {  
  12.     private EditText name,id,sex,age;  
  13.     private Button button;  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         name = (EditText)findViewById(R.id.editname);  
  20.         id = (EditText)findViewById(R.id.editage);  
  21.         sex = (EditText)findViewById(R.id.editsex);  
  22.         age = (EditText)findViewById(R.id.editage);  
  23.         button = (Button)findViewById(R.id.next);  
  24.         button.setOnClickListener(new OnClickListener() {  
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 // TODO Auto-generated method stub  
  28.                 String namestr = name.getText().toString();  
  29.                 String idstr = id.getText().toString();  
  30.                 String sexstr = sex.getText().toString();  
  31.                 String agestr = age.getText().toString();  
  32.                 Person person = new Person();  
  33.                 person.setAge(agestr);  
  34.                 person.setId(idstr);  
  35.                 person.setName(namestr);  
  36.                 person.setSex(sexstr);  
  37.                 Bundle bundle = new Bundle();  
  38.                 bundle.putSerializable("personObject", person);  
  39.                 Intent intent = new Intent(ObjectPassDemoActivity.this, ResultActivty.class);  
  40.                 intent.putExtras(bundle);  
  41.                 startActivity(intent);  
  42.             }  
  43.         });  
  44.     }  
  45. }  


 

[java]  view plain copy
  1. package cn.com.wd;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.   
  8. public class ResultActivty extends Activity {  
  9.     private TextView name,age,id,sex;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         // TODO Auto-generated method stub  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.result);  
  15.         name = (TextView)findViewById(R.id.name);  
  16.         age = (TextView)findViewById(R.id.age);  
  17.         id = (TextView)findViewById(R.id.id);  
  18.         sex = (TextView)findViewById(R.id.sex);  
  19.         Intent intent = this.getIntent();  
  20.         Bundle bundle = intent.getExtras();  
  21.         Person person = (Person)bundle.getSerializable("personObject");  
  22.         name.setText(person.getName());  
  23.         age.setText(person.getAge());  
  24.         id.setText(person.getId());  
  25.         sex.setText(person.getSex());  
  26.     }  
  27.         
  28. }  


(2)Parcelable

    

[java]  view plain copy
  1. package cn.com.PracelAbleTest;  
  2.   
  3. import java.util.HashMap;  
  4.   
  5. import android.os.Parcel;  
  6. import android.os.Parcelable;  
  7.   
  8. public class Person implements Parcelable {  
  9.     public HashMap<String, Object> map = new HashMap<String, Object>();  
  10.     public String name;  
  11.   
  12.     @Override  
  13.     public int describeContents() {  
  14.         // TODO Auto-generated method stub  
  15.         return 0;  
  16.     }  
  17.   
  18.     @Override  
  19.     public void writeToParcel(Parcel dest, int flags) {  
  20.         // TODO Auto-generated method stub  
  21.         dest.writeMap(map);  
  22.         dest.writeString(name);  
  23.     }  
  24.   
  25.     public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {  
  26.   
  27.         @Override  
  28.         public Person createFromParcel(Parcel source) {  
  29.             // TODO Auto-generated method stub  
  30.             Person p = new Person();  
  31.             p.map = source.readHashMap(HashMap.class.getClassLoader());  
  32.             p.name = source.readString();  
  33.             return p;  
  34.         }  
  35.   
  36.         @Override  
  37.         public Person[] newArray(int size) {  
  38.             // TODO Auto-generated method stub  
  39.             return null;  
  40.         }  
  41.     };  
  42. }  


 

[java]  view plain copy
  1. package cn.com.PracelAbleTest;  
  2.   
  3. import java.util.HashMap;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.os.Bundle;  
  10.   
  11. public class ParcelableDemoActivity extends Activity {  
  12.     private Bitmap bitmap;  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
  19.         Intent intent = new Intent();  
  20.         Person p = new Person();  
  21.         p.map = new HashMap<String, Object>();  
  22.         p.map.put("key","ido");  
  23.         p.map.put("img", bitmap);  
  24.         p.name = "ok";  
  25.         intent.putExtra("key",p);  
  26.         intent.setClass(ParcelableDemoActivity.this,NextDemo.class);  
  27.         startActivity(intent);  
  28.     }  
  29. }  


 

[java]  view plain copy
  1. package cn.com.PracelAbleTest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.graphics.Bitmap;  
  6. import android.os.Bundle;  
  7. import android.widget.ImageView;  
  8.   
  9. public class NextDemo extends Activity{  
  10.     ImageView iv ;  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         // TODO Auto-generated method stub  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         iv = (ImageView)findViewById(R.id.image);  
  17.         Intent i = getIntent();  
  18.         Person p = i.getParcelableExtra("key");  
  19.         System.out.println("----->"+p.name);  
  20.         System.out.println("----->"+p.map.get("key"));  
  21.         Bitmap bitmap = (Bitmap) p.map.get("img");  
  22.         iv.setImageBitmap(bitmap);  
  23.     }  
  24.   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值