Activity之间通过Intent传递值,支持基本数据类型和String对象及 它们的数组对象byte、byte[]、char、char[]、boolean、boolean[]、short、short[]、int、 int[]、long、long[]、float、float[]、double、double[]、String、String[],还有采用实现 Serializable、Parcelable接口的类对象传递数据的两种方法:一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object)

例如:

  1. import android.os.Parcel;
  2. import android.os.Parcelable;
  3. public class Book implements Parcelable {
  4. private String bookName;
  5. private String author;
  6. private int publishTime;
  7. public String getBookName() {
  8. return bookName;
  9. }
  10. public void setBookName(String bookName) {
  11. this.bookName = bookName;
  12. }
  13. public String getAuthor() {
  14. return author;
  15. }
  16. public void setAuthor(String author) {
  17. this.author = author;
  18. }
  19. public int getPublishTime() {
  20. return publishTime;
  21. }
  22. public void setPublishTime(int publishTime) {
  23. this.publishTime = publishTime;
  24. }
  25. public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
  26. public Book createFromParcel(Parcel source) {
  27. Book mBook = new Book();
  28. mBook.bookName = source.readString();
  29. mBook.author = source.readString();
  30. mBook.publishTime = source.readInt();
  31. return mBook;
  32. }
  33. public Book[] newArray(int size) {
  34. return new Book[size];
  35. }
  36. };
  37. public int describeContents() {
  38. return 0;
  39. }
  40. public void writeToParcel(Parcel parcel, int flags) {
  41. parcel.writeString(bookName);
  42. parcel.writeString(author);
  43. parcel.writeInt(publishTime);
  44. }
  45. }

实现Serializable接口:

  1. import java.io.Serializable;
  2. public class Person implements Serializable {
  3. private static final long serialVersionUID = -7060210544600464481L;
  4. private String name;
  5. private int age;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public int getAge() {
  13. return age;
  14. }
  15. public void setAge(int age) {
  16. this.age = age;
  17. }
  18. }

这样在Activity中就可编写传递数据代码:

  1. Intent mIntent = new Intent(this, 地址Activity.class);
  2. Bundle mBundle = new Bundle();
  3. //采用parcelable传输数据
  4. mBundle.putParcelable("data", mBook);
  5. //采用serializable传输数据
  6. mBundle.putSerializable("data", items);
  7. mIntent.putExtras(mBundle);
  8. startActivity(mIntent);

接受数据可以采用:

    1. Intent intent = getIntent();
    2. ArrayList<Map<String, String>> items = (ArrayList<Map<String, String>>)intent.getExtras().get("data");

Android 开发中使用Intent传递数据的方法的更多相关文章

  1. Android开发中Bundle用法包裹数据(转)

    Android开发中Bundle用法包裹数据 Bundle的经典用法,包裹数据放入Intent中,目的在于传输数据. SDK 里是这样描述: A mapping from String values ...

  2. Android程序中Acticity间传递数据

    在Android开发过程中,在不同的Acitivity之间传递数据的情况是非常常见的.我花费了一点时间来总结Acitivity之间的数据传递,记录下来. 1.简单传递键值对 这种传递方式非常简单,只需 ...

  3. Android 开发笔记——通过 Intent 传递类对象

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

  4. Android开发中使用Intent跳转到系统应用中的拨号界面、联系人界面、短信界面

    现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 首先,我们先看拨号界面,代码如下: Intent intent =new Intent(); intent. ...

  5. Android TabHost中Activity之间传递数据

    例子1: TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost); tabhost.setup(this.getLocalActi ...

  6. Android开发中完全退出程序的三种方法

    参考: http://android.tgbus.com/Android/tutorial/201108/363511.shtml Android程序有很多Activity,比如说主窗口A,调用了子窗 ...

  7. Intent传递数据的方法

    一.传递List 1.传递List<String>的方法 ArrayList<String> info = new ArrayList<String>(); inf ...

  8. Xamarin Android 中Acitvity如何传递数据

    在xamarin android的开发中,activity传递数据非常常见,下面我也来记一下在android中activity之间传递数据的几种方式, Xamarin Android中Activity ...

  9. Intent 对象在 Android 开发中的应用

    转自(http://www.ibm.com/developerworks/cn/opensource/os-cn-android-intent/) Android 是一个开放性移动开发平台,运行在该平 ...

随机推荐

  1. Python 时间整理

    在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...

  2. RHadoop教程翻译系列 _Mapreduce(1)_第一个Mapreduce任务

    如果单从概念上来说,Mapreduce和R中的函数lapply, tapply并无差别,它们都是把元素转化成列,然后计算索引(Mapreduce中的键),最后合并成一个定义好的组合.首先,让我们看一个 ...

  3. vmware下ubuntu14.04调整分辨率

    很多人在vmware中安装ubuntu时,为了调整屏幕分辨率,都去下载并安装vmware-tools.其实,这是没有必要的.如果你需要vmware和宿主机实现共享,或者为了使文件能拖进拖出,再或者是需 ...

  4. Mapreduce执行过程分析(基于Hadoop2.4)——(一)

    1 概述 该瞅瞅MapReduce的内部运行原理了,以前只知道个皮毛,再不搞搞,不然怎么死的都不晓得.下文会以2.4版本中的WordCount这个经典例子作为分析的切入点,一步步来看里面到底是个什么情 ...

  5. JIT(动态编译)和AOT(静态编译)编译技术比较

    Java 应用程序的性能经常成为开发社区中的讨论热点.因为该语言的设计初衷是使用解释的方式支持应用程序的可移植性目标,早期 Java 运行时所提供的性能级别远低于 C 和 C++ 之类的编译语言.尽管 ...

  6. Cocos2d-JS v3.0 alpha不支持cocos2d-x的Physics integration

    cocos2d-x 3.0新的Physics integration,把chipmunk和Box2D封装到引擎内部 auto scene = Scene::createWithPhysics(); s ...

  7. HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception

    HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception

  8. UVALive 3959 Rectangular Polygons (排序贪心)

    Rectangular Polygons 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/G Description In thi ...

  9. Unity3D Keynote

    [Unity3D Keynote] 1.场景文件扩展名为.unity. 2.up为Y正方向,down为Y负方向,right为X正方向,left为X负方向,forward为Z正方向,back为z负方向. ...

  10. C++11用于计算函数对象返回类型的统一方法

    [C++11用于计算函数对象返回类型的统一方法] 模板 std::result_of 被TR1 引进且被 C++11 所采纳,可允许我们决定和使用一个仿函数其回返值的类别.底下,CalculusVer ...