Android Bundle传递数据
1.传递普通数据
Intent intent=new Intent(MainActivity.this,TwoActivity.class);
Bundle bundle=new Bundle();
bundle.putString("name","张三");
bundle.putInt("age",18);
bundle.putString("gender","男");
intent.putExtras(bundle);
startActivity(intent);
获取传递的数据
Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name");
String gender=bundle.getString("gender");
int age =bundle.getInt("age");
2.传递Serializable数据
1.创建一个类实现Serializable
2.传递数据
Intent intent=new Intent(MainActivity.this,TwoActivity.class);
Bundle bundle=new Bundle();
Person1 p1=new Person1("张三","男",18);
bundle.putSerializable("person",p1);
intent.putExtras(bundle);
startActivity(intent);
3.接受数据
Bundle bundle=getIntent().getExtras();
Person1 p1= (Person1) bundle.getSerializable("person");
String name=p1.getName();
String gender=p1.getGender();
int age =p1.getAge();
3.传递Parcelable数据
1.创建类实现Parcelabel
public class Person3 implements Parcelable {
private String name;
private String gender;
private int age;
public Person3(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person3{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
public static final Parcelable.Creator<Person3> CREATOR=new Parcelable.Creator<Person3>(){
/**
* 供外部类反序列话本类数组使用
* @param source
* @return
*/
@Override
public Person3 createFromParcel(Parcel source) {
return new Person3(source);
}
/**
* 从Parcel中读取数据
* @param size
* @return
*/
@Override
public Person3[] newArray(int size) {
return new Person3[size];
}
};
/**
* 默认返回0就行
* @return
*/
@Override
public int describeContents() {
return 0;
}
/**
* 把值写进Parcel中
* @param dest
* @param flags
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(gender);
dest.writeInt(age);
}
/**
* 这里的读取数据必须与writeToParacel(Parcel dest,int flags)一致,否则就会出错
* @param source
*/
public Person3(Parcel source) {
name = source.readString();
gender=source.readString();
age = source.readInt();
}
}
2.传递数据
Intent intent=new Intent(MainActivity.this,TwoActivity.class);
Bundle bundle=new Bundle();
Person3 p3=new Person3("张三","男",18);
bundle.putParcelable("person",p3);
intent.putExtras(bundle);
startActivity(intent);
3.接受数据
Bundle bundle=getIntent().getExtras();
Person3 p3= bundle.getParcelable("person");
String name=p3.getName();
String gender=p3.getGender();
int age =p3.getAge();
Android Bundle传递数据的更多相关文章
- android bundle存放数据详解
转载自:android bundle存放数据详解 正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中 例如: 将数据存放倒Intent中传递: 将数据放到Bun ...
- Bundle传递数据,Handler更新UI
Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的. Bundle经常使用在Activity之间或者线程间传递数据,传递的数据可以是boolean.byte.in ...
- Android Bundle传递简单数据、对象数据
Android开发过程中进程遇到组件之间.进程之间等数据的传递,数据传递有非常多种,当中使用Bundle传递非常方便. Bundle能够传递多种数据,是一种类似map的key-value数据结构 简单 ...
- Android之间传递数据包
在Android中 ,我们知道,两个activity之间通讯要用到Intent类,传递简单数据的方式我们也已经知道了.那么,如何在两个activity之间传递数据包呢,这就要用到我们的Bundle类了 ...
- 关于Android中传递数据的一些讨论--备用
在Android中编写过程序的开发人员都知道.在Activity.Service等组件之间传递数据(尤其是复杂类型的数据)很不方便.一般可以使用Intent来传递可序列化或简单类型的数据.看下面的代码 ...
- 关于Android中传递数据的一些讨论
在Android中编写过程序的开发人员都知道.在Activity.Service等组件之间传递数据(尤其是复杂类型的数据)很不方便.一般可以使用Intent来传递可序列化或简单类型的数据.看下面的代码 ...
- Android开发—— 传递数据
一:使用静态变量传递数据 (1)静态变量传递数据,在目标Activity中声明静态变量,然后使用setText()方法将静态变量的值导出即可: (2)静态变量传递数据,在主Activity中对目标Ac ...
- Activity通过bundle传递数据
从AActivity.java向BActivity.java传递数据: 建立AActivity.java文件建立bundle: 1 public class AActivity extends App ...
- Android Intent传递数据
刚开始看郭大神的<>,实现以下里面的一些例子.Intent传递数据. 我们利用显示的方式进行Intent的启动. 1.启动intent并输入数据. Intent intent=new In ...
随机推荐
- Linux bash shell 入门
https://www.cnblogs.com/cosiray/archive/2012/03/02/2377099.html
- 自定义第三方YUM源
1.切换到cloudboot系统目录中 2.拷贝repodata目录的*-repo.xml文件到系统目录下 3.删除系统目录的repodata目录 4.编辑repo.xml内容,base添加rpm包包 ...
- 【转】使用Jmeter针对ActiveMQ JMS Point To Point压力测试
准备工作 针对JMS类型的Sampler,需要额外的jar包(这里用的是apache ActiveMQ,将下载的AMQ apache-activemq-5.5.0根目录下的activemq-all-5 ...
- iOS平台下闪退原因汇总(一):"Ran out of trampolines of type 0/1/2" 运行时间错误
"Ran out of trampolines of type 0/1/2" 运行时间错误通常出现在使用大量递归泛型时.要看到这个错误需要连接着设备直接将项目build到设备里运行 ...
- C#获取视频文件播放长度
下面两种方法只支持部分视频格式,一般格式mp3,wma等等支持 1.使用Shell32 添加引用,选择COM中的Microsoft Shell Controls And Automation引用 // ...
- Select/Poll/Epoll异步IO
IO多路复用 同步io和异步io,阻塞io和非阻塞io分别是什么,有什么样的区别? io模式 对于一次io 访问(以read为例),数据会先拷贝到操作系统内核的缓冲区,然后才会从操作系统内核的缓冲区拷 ...
- 【技术调研】最强Node-RED初探总结
在某个项目中需要调研下node-red的功能,我大概花了三天时间研究了相关的官方文档,写了几个Demo总结了下node-red相关的功能.如需转载,请注明出处 https://www.cnblogs. ...
- Mycat实战之主键数据库自增方式
创建一个 person表,主键为Id,hash方式分片,主键自增(采用数据库方式) #person表结构如下 Id,主键,Mycat自增主键 name,字符串,16字节最长 school,毕业学校,数 ...
- Mycat实战之新增基于hash分片的表
1. 修改rule.xml hash分片规则 主要改两个地方: vi rule.xml 分片数量,这里改为3 对应 三个库 hash规则 默认是id列 这里为 PROVINCE 2. reload 加 ...
- Hadoop Serialization -- hadoop序列化详解 (3)【ObjectWritable,集合Writable以及自定义的Writable】
前瞻:本文介绍ObjectWritable,集合Writable以及自定义的Writable TextPair 回顾: 前面了解到hadoop本身支持java的基本类型的序列化,并且提供相应的包装实现 ...