1. public class SerializableBook implements Serializable {
  2. private static final long serialVersionUID = 4226755799531293257L;
  3. private String Name;
  4. private String Author;
  5. private String Pubdate;
  6. private float Price;
  7. public void setName(String name) {
  8. Name = name;
  9. }
  10. public String getName() {
  11. return Name;
  12. }
  13. public void setAuthor(String author) {
  14. Author = author;
  15. }
  16. public String getAuthor() {
  17. return Author;
  18. }
  19. public void setPubdate(String pubdate) {
  20. Pubdate = pubdate;
  21. }
  22. public String getPubdate() {
  23. return Pubdate;
  24. }
  25. public void setPrice(float price) {
  26. Price = price;
  27. }
  28. public float getPrice() {
  29. return Price;
  30. }
  31. }

然后起一个Activity A,这都是和之前的Activity介绍的例子一样,代码如下:

Java代码
  1. public class ActivityA extends Activity {
  2. private String SerializableKey = "ourunix_serialzable";
  3. private Button mButton;
  4. @Override
  5. public void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.layout_for_a);
  8. initView();
  9. mButton.setOnClickListener(new OnClickListener() {
  10. @Override
  11. public void onClick(View v) {
  12. // TODO Auto-generated method stub
  13. tranSerializableObject();
  14. }
  15. });
  16. }
  17. public void initView(){
  18. mButton = (Button) findViewById(R.id.a_button);
  19. mButton.setText("A跳B");
  20. }
  21. public void tranSerializableObject(){
  22. Intent in = new Intent();
  23. in.setClass(ActivityA.this, ActivityB.class);
  24. //实例化一个SerializableBook对象
  25. SerializableBook book = new SerializableBook();
  26. book.setAuthor("walfred");
  27. book.setName("How to learn Android");
  28. book.setPrice(10.00f);
  29. book.setPubdate("2014-01-01");
  30. Bundle extras = new Bundle();
  31. extras.putSerializable(SerializableKey, book);
  32. in.putExtras(extras);
  33. startActivity(in);
  34. }
  35. }

最后在Activity B中接受这个对象,并展示出来,代码如下:

Java代码
  1. public class ActivityB extends Activity {
  2. private String SerializableKey = "ourunix_serialzable";
  3. private TextView mTextView;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. // TODO Auto-generated method stub
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.layout_for_b);
  9. initView();
  10. getAndShowSerialzableObeject();
  11. }
  12. public void initView(){
  13. mTextView = (TextView)findViewById(R.id.b_textview);
  14. }
  15. public void getAndShowSerialzableObeject(){
  16. Bundle extras = getIntent().getExtras();
  17. if (extras != null){
  18. SerializableBook book = (SerializableBook) extras.get(SerializableKey);
  19. mTextView.setText("Name:" + book.getName()+"\n"
  20. + "Author:" + book.getAuthor() + "\n"
  21. + "Pubdate:" + book.getPubdate() + "\n"
  22. + "Price:" + book.getPrice());
  23. }else{
  24. mTextView.setText("nothing");
  25. }
  26. }
  27. }

Activity使用Serializable传递对象实例的更多相关文章

  1. intent使用Serializable传递对象

    package com.pingyijinren.test; import android.content.Intent; import android.support.v7.app.AppCompa ...

  2. 【转】Android中如何使用Bundle传递对象[使用Serializable或者Parcelable] -- 不错

    原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1211/694.html Android中Bundle类的作用 Bun ...

  3. 在Android中通过Intent使用Bundle传递对象

    IntentBundle传递对象SerializableParcelable Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法.被传递的对象 ...

  4. Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]

    http://blog.csdn.net/cjjky/article/details/6441104 在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSeri ...

  5. [Android学习]Activity之间传递对象和对象集合

    开发过程中,Activity之间传递数据是必不可少的,android中使用Intent和Bundle作为数据载体,在Activity之间传递,对于基础数据类型,Bundle已经提供相关的put,get ...

  6. Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...

  7. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  8. 利用Bundle在activity之间传递对象

    (2010-12-04 09:45:54) 转载▼ 标签: it 分类: android开发 转自:http://chen592969029.javaeye.com/blog/772656 假如需要在 ...

  9. Intent传递对象——Serializable和Parcelable区别

    为什么要将对象序列化? 1.永久性保存对象,保存对象的字节序列到本地文件中: 2.用过序列化对象在网络中传递对象: 3.通过序列化对象在进程间传递对象. 1.实现Serializable接口 Seri ...

随机推荐

  1. Linux如何删除以分号开头的文件

    发现在创建文件时,有的时候会不小心创建以分号开头的文件. 如何删除呢? 执行  rm \;   即可删除 把以;号开头的文件名转义后再删除 创建文件:vi  index.php 或者vim  inde ...

  2. 唐巧的iOS技术博客选摘

    1. 那些被遗漏的objective-c保留字:http://blog.devtang.com/blog/2013/04/29/the-missing-objc-keywords/   2. 使用cr ...

  3. VS2010 使用时选择代码或双击时出错,点击窗口按钮后VS自动重启问题

    VS2010 使用时选择代码或双击时出错崩溃,点击窗口按钮后VS自动重启问题 下载补丁,打上补丁之后,重启电脑,解决了问题. WindowsXP的下载地址:Windows XP 更新程序 (KB971 ...

  4. PPTP服务器配置选项详解

    导读 PPTP服务器配置文件的格式与其它许多Unix程序相似,每一行包含一项配置内容,以配置选项名称开始,后面紧跟参数值或者关键字,它们之间用空格分隔.在读取配置文件时,pptpd进程将忽略空行和每一 ...

  5. bitbucket新建分支

    /workspace/ott_app_store:fetch-xml$ git branch edit_package_page /workspace/ott_app_store:fetch-xml$ ...

  6. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  7. 重写HashMap

    package gov.gfmis.fap.util; import java.util.ArrayList; import java.util.HashMap; import java.util.L ...

  8. Stanford机器学习---第七讲. 机器学习系统设计

    原文:http://blog.csdn.net/abcjennifer/article/details/7834256 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  9. 【ERROR】使用jquery的ajax出现error:readyState=4,status=500

    使用jquery的ajax出现error:readyState=4,status=500,ajax代码如下: $.ajax({ url : "../toBeFinMisManage/show ...

  10. 速度之王 — LZ4压缩算法(三)

    LZ4使用 make / make clean 得到可执行程序:lz4.lz4c Usage: ./lz4 [arg] [input] [output] input : a filename Argu ...