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. 各种工具使用手册:http://www.itshouce.com.cn/linux/linux-tcpdump.html 关于tcpdump!!!!

    各种工具使用手册:http://www.itshouce.com.cn/linux/linux-tcpdump.html 关于tcpdump!!!! 实用tcpdump命令 //查看本机与mysql的 ...

  2. 编译安装apache-2.4.18

    apache安装时, 必须要apr和apr-util, 这两个包是必须的 当下载apache的版本过高, 如: apache-2.4.18, 那么要求的apr或apu=apr-util版本将至少在1. ...

  3. gvim-ide plugins

    omnicppcomplete-0.41.zip after: ftplugin: c.vim,cpp.vim,两个文件的内容相同, 其中都只有一个call语句: call omni#cpp#comp ...

  4. 【原创】angularjs1.3.0源码解析之scope

    Angular作用域 前言 之前我们探讨过Angular的执行流程,在一切准备工作就绪后(我是指所有directive和service都装载完毕),接下来其实就是编译dom(从指定的根节点开始遍历do ...

  5. git 笔记- 概念

    本文参考书中内容 http://cnpmjs.org/ 镜像文件 下载插件的镜像 可参考fis 对于任何一个文件,在Git 内都只有三 种状态:已提交(committed),已修改(modified) ...

  6. 《Hadoop基础教程》之初识Hadoop

    Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. <Hadoop基础教程> ...

  7. linux查看python安装路径,版本号

    一.想要查看ubuntu中安装的Python路径 方法一:whereis python 方法二:which python 二.想要查看ubuntu中安装的python版本号 python

  8. ZeroMQ(java)之I/O线程的实现与组件间的通信

    算是开始读ZeroMQ(java)的代码实现了吧,现在有了一个大体的了解,看起来实现是比较的干净的,抽象什么的不算复杂... 这里先来看看它的I/O线程的实现吧,顺带看看是如何实现组件的通信的.... ...

  9. ThreadLocal的设计与使用(原理篇)

    在jdk1.2推出时开始支持java.lang.ThreadLocal.在J2SE5.0中的声明为:            public class ThreadLocal<T> exte ...

  10. Kth Largest Element in an Array

    Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...