`
hm4123660
  • 浏览: 277881 次
  • 性别: Icon_minigender_1
  • 来自: 广州
博客专栏
Dea4ce76-f328-3ab2-b24a-fb268e1eeb75
数据结构
浏览量:68975
社区版块
存档分类
最新评论

Bundle自定义数据传递

阅读更多

      我们都知道Bundle可能过put****()方法添加各种基本类型的数据,Intent也可以通过putExtras(Bundle)将数据添加进去,然后通过startActivity()跳到下一下Activity的时候就把数据也传到下一个Activity了。如传递一个字符串到下一个Activity

 

把数据放到Intent里

 btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
           public void onClick(View v) {

                Intent intent1=new Intent(MyActivity.this,SecondActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("title","Activity 2");//把Activity 2传给下一个Activity
                intent1.putExtras(bundle);
                startActivity(intent1);
            }
 });

 

接收端Activity代码为:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test);
        //获取bundle数据
        Bundle bundle=getIntent().getExtras();
        String text=bundle.getString("title");//根据key来获取
        TextView title=(TextView)findViewById(R.id.title);
        title.setText(text+"   "+person.getName()+"  "+person.getAge());
    }

 

上面传递的是基本数据类型的数据,现在主要来说明自定义数据类型的传递。

 

1。序列化(详情参看java的序列化

 

定义Person类实现Serializable接口,Serializable接口没有定义任何方法,是一个标记接口

package com.example.AndroidStudy;

import java.io.Serializable;

public class Person  implements Serializable{

    private String name;

    private int age;//可用transient修饰不序列化某属性

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

使用 Bundle.putSerializable()方法把自定义Person数据放入Bundle内

 Intent intent1=new Intent(MyActivity.this,SecondActivity.class);
 Bundle bundle=new Bundle();
 bundle.putString("title","Activity 2");
 bundle.putSerializable("person",new Person("小白",20));
 intent1.putExtras(bundle);
 startActivity(intent1);

 

使用Bundle.getSerializable()来获取自定义类型数据

 //获取bundle数据
 Bundle bundle=getIntent().getExtras();
 String text=bundle.getString("title");//根据key来获取
 Person person=(Person)bundle.getSerializable("person");

 

2.实现Parcelable接口

     Parcelable是Android特有的功能,效率要比实现Serializable接口高。但实现Parcelable稍微复杂一些,推荐使用这种方法。

 

1.自定义Person类实现 Parcelable接口

package com.example.AndroidStudy;

import android.os.Parcel;
import android.os.Parcelable;

public class Person  implements Parcelable{

    private String name;

    private   int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //重写下面两个方法
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //把数据写入Parcel
        dest.writeString(name);
        dest.writeInt(age);
    }

    //还必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法

    public static final  Creator<Person>CREATOR=new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel source) {
            //读取时返回Person对象---根据Parcel写入的数据生成Person返回
            return new Person(source.readString(), source.readInt());
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}

 

 使用 Bundle.putParcelable()方法把自定义Person数据放入Bundle内

Intent intent1=new Intent(MyActivity.this,SecondActivity.class);
Bundle bundle=new Bundle();
bundle.putString("title","Activity 2");
bundle.putParcelable("person",new Person("小白",20));
intent1.putExtras(bundle);
startActivity(intent1);

 

使用Bundle.getParcelable()来获取自定义类型数据

 //获取bundle数据
 Bundle bundle=getIntent().getExtras();
 String text=bundle.getString("title");//根据key来获取
 Person person=(Person)bundle.getParcelable("person");

 

 

 

3
0
分享到:
评论

相关推荐

    Bundle传递数据方式

    包含Bundle两种传递数据方式,一种是传递简单数据,另一种是传递自定义数据

    Android 不同Activity间数据的传递 Bundle对象的应用

    在应用中,可能会在当跳转到另外一个Activity的时候需要传递数据过去,这时就可能用Bundle对象; 在MainActivity中,有一个导航至BActivity的Intent, Intent 代码如下:{  Intent intent = new Intent(Context ...

    Android中传递对象

    我们知道在Android 系统中,可以用Bundle或Intent来保存和传递数据。我们有时由于程序某种需要,可能需要传递各种各样的自定义对象,只传递简单类型的数据是不够的。但这些对象在Activity之间不能直接传递,用什么...

    gtfs-meta:ActiveRecord 的 GTFS 数据和元数据管理器。 提取提要数据、管理提要版本并扩展提要规范以包括提要元数据

    GTFS::元ActiveRecord 的数据和元数据管理器。 提取提要数据、管理提要版本并扩展提要规范以包括提要元数据。安装将此行添加到应用程序的 Gemfile 中: ... migrate ( :up )饲料播种将自定义种子数据传递给以将有效的提

    ougai:Ruby结构化日志记录能够轻松处理消息,自定义数据或异常,并生成JSON或人类可读的日志

    结构化日志系统能够轻松处理消息,结构化数据或异常。 它具有与Node.js的或兼容的JSON格式化程序,以及与兼容的控制台可读格式。 安装 将此行添加到您的应用程序的Gemfile中: gem 'ougai' 然后执行: $ bundle ...

    Android编程实现使用Intent传输包含自定义类的ArrayList示例

    之前项目中通过Intent只是传输简单的字符串,这次因为需要在前一个页面联网获取对象数据,然后在下一个页面使用,所以考虑到使用Intent传输包含自定义类的ArrayList。 Serializable Java的对象序列化指的是将那些...

    Familio:GwG项目4

    该应用程序的目的是探索创建自定义阵列适配器,存储值和使用Intent。 最大的挑战是在Activity之间来回移动,因为我们正在通过putExtra()传递信息。 此方法是Intent类的一部分,该类具有两个输入,一个String键和...

    ogrtool:OGR 工具的命令行实用程序包装器

    在这里您可以定义自定义 Postgres 数据库连接,因此您不必每次都使用原始ogr2ogr传递它们。 修改和添加任何 Postgres 服务器的连接参数。 然后安装(默认为~/local/ogrtool ): bundle make 并将该位置添加到您...

    Android开发艺术探索

     2.4.1 使用Bundle / 61  2.4.2 使用文件共享 / 62  2.4.3 使用Messenger / 65  2.4.4 使用AIDL / 71  2.4.5 使用ContentProvider / 91  2.4.6 使用Socket / 103  2.5 Binder连接池 / 112  2.6 选用合适的...

    android 面试2

    16、Intent传递数据时,下列的数据类型那些可以被传递(ABCD) A、Serializable B、charsequence C、Parcelable(邮包类型) D、Bundle 17、Android中使用Menu时可能需要重写的方法(AC) A、onCreateOptionsMenu() B、...

    Android开发艺术探索.任玉刚(带详细书签).pdf

    2.4.1 使用Bundle 61 2.4.2 使用文件共享 62 2.4.3 使用Messenger 65 2.4.4 使用AIDL 71 2.4.5 使用ContentProvider 91 2.4.6 使用Socket 103 2.5 Binder连接池 112 2.6 选用合适的IPC方式 121 第3章 View的...

    ch_digest:ch_digest

    一个自定义工具,用于从Clubhouse提取导出的数据。 Ruby要求 该程序已经过Ruby 2.5.3的测试,并且可以与任何Ruby 2.x版本一起使用,并且可能与更高版本一起使用。 跑步 使用Ruby解释器(2.5.3或更高版本)运行文件...

    android开发艺术探索高清完整版PDF

    / 42 2.3.1 Serializable接口 / 42 2.3.2 Parcelable接口 / 45 2.3.3 Binder / 47 2.4 Android中的IPC方式 / 61 2.4.1 使用Bundle / 61 2.4.2 使用文件共享 / 62 2.4.3 使用Messenger / 65 2.4.4 使用AIDL ...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个Activity——startActivityForResult方法 3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个Activity——startActivityForResult方法 3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个Activity——startActivityForResult方法 3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView...

    疯狂Android讲义源码

     4.1.4 使用Bundle在Activity之间  交换数据 181  4.1.5 启动其他Activity并返回结果 185  4.2 Activity的回调机制 189  4.3 Activity的生命周期 190  4.3.1 Activity的生命周期演示 190  4.3.2 Activity与...

    Android 开发技巧

    2.1.4、Activity 的跳转(含Bundle传值) 17 2.1.5.Actvity 堆栈 18 2.1.6、Intent对象调用Activity实例 19 2.1.7、Activity透明 21 2.1.8、一次性关闭所有的Activity 22 2.1.9、PreferenceActivity 用法 22 2.1.10...

Global site tag (gtag.js) - Google Analytics