当前位置: 首页 > news >正文

Android 使用Serialiable接口和Parcelable接口进行数据传送

一、前言

这篇文章主要针对Serialiable和Parcelable接口来传递对象。呈现的功能是跳转到另一个界面,然后通过toast展现我收到的数据。

二、使用Serialiable接口传递数据

1.创建需要传递的对象

//必须实现Serializable接口,此对象才有传递的资格
public class Student implements Serializable {public int id;public String name;public int age;}

2.传数据

   /*** 跳转到Serialiable2Activity* @param view*/public void startActivity(View view) {Intent intent = new Intent(this, Serialiable2Activity.class);//传递对象到Serialiable2ActivityStudent student = new Student();student.id = 9;student.name = "Anglin";student.age = 33;intent.putExtra("student",student);startActivity(intent);}

3.接收数据

Intent intent = getIntent();Student student = (Student) intent.getSerializableExtra("student");//提示显示Toast.makeText(this, "student.id" + student.id +"student.name"+student.name + "student.age" + student.age,Toast.LENGTH_SHORT).show();

二、使用Parcelable接口传递数据

1.创建需要传递的数据对象

//成为Parcelable的子类,就具备传递数据的资格
public class ParcelableStudent implements Parcelable {public ParcelableStudent() {}//我们自己定义的成员public String name;public int age;//TODO 读取的数据和写入的数据一定要一致否则会报错//从Parcel对象里面读出来,赋值给成员//构造函数protected ParcelableStudent(Parcel in) {//这个函数的意义就是从Parcel读取数据赋值给name和agename = in.readString();age = in.readInt();}//把属性写入到Parcel 对象中去@Overridepublic void writeToParcel(@NonNull Parcel parcel, int i) {parcel.writeString(name);parcel.writeInt(age);}//先不管,是系统扩展用的@Overridepublic int describeContents() {return 0;}//静态公开的成员,Parcelable内部会调用。  一定要有 自动生成 或者从文档中复制不需要去写。public static final Creator<ParcelableStudent> CREATOR = new Creator<ParcelableStudent>() {//创建ParcelableStudent对象  并且Parcel对象构建好传递给ParcelableStudent(成员数据就可以从Parcel获取了)@Overridepublic ParcelableStudent createFromParcel(Parcel in) {return new ParcelableStudent(in);}//@Overridepublic ParcelableStudent[] newArray(int size) {return new ParcelableStudent[size];}};
}

2.传送数据

  public void startActivity(View view) {Intent intent = new Intent(this,Parcelable2Activity.class);ParcelableStudent student = new ParcelableStudent();student.age = 20;student.name = "Anglin";intent.putExtra("student",student);startActivity(intent);}

3.接收数据

    Intent intent = getIntent();ParcelableStudent student = intent.getParcelableExtra("student");//显示Parcelable1Activity 传递过来的对象 里面的数据Toast.makeText(this, "student.name" + student.name + "student.age" + student.age, Toast.LENGTH_SHORT).show();

四、总结

那么在编写android代码 的时候这两个我们改选择使用哪个呢?一般我们会选择使用Parcelabe,因为parcelable的性能更高。Serialiable面向jvm使用java序列化的形式传递对象的。android平台的虚拟机更适合Parcelable。

所以android开发必须使用Parcelabe因为这个是支持  兼容安卓虚拟机。我们主推的方式

http://www.lryc.cn/news/267348.html

相关文章:

  • 【数据结构入门精讲 | 第十七篇】一文讲清图及各类图算法
  • Python 直方图的绘制-`hist()`方法(Matplotlib篇-第7讲)
  • Quartz持久化(springboot整合mybatis版本实现调度任务持久化)--提供源码下载
  • 掌握的单词个数 - 华为OD统一考试
  • 如何使用ArcGIS Pro将Excel表转换为SHP文件
  • 11.1Linux串口应用程序开发
  • log4j学习
  • 【Vue2+3入门到实战】(4)Vue基础之指令修饰符 、v-bind对样式增强的操作、v-model应用于其他表单元素 详细示例
  • 【数据结构和算法】找到最高海拔
  • redis相关问题
  • 第41节: Vue3 watch函数
  • Centos7:升级gcc、g++到版本5.2.0
  • Pytohn data mode plt
  • 内网离线搭建之----kafka集群
  • 5.1 显示窗口的内容(一)
  • 基于包围盒算法的三维点云数据压缩和曲面重建matlab仿真
  • 关于Python里xlwings库对Excel表格的操作(十八)
  • VScode远程连接服务器,Pycharm专业版下载及远程连接(深度学习远程篇)
  • Vue2和Vue3组件间通信方式汇总(3)------$bus
  • PyTorch加载数据以及Tensorboard的使用
  • TensorFlow是什么
  • docker-compose 安装Sonar并集成gitlab
  • 支付平台在选择服务器租用时要注意什么?
  • IDEA2018升级2023,lombok插件不兼容导致get/set方法无法使用
  • 企业微信服务商代开发模式获取授权企业的客户信息
  • 库存管理方法有哪些
  • 数字化车间推动制造业生产创新
  • Linux的安装及管理程序
  • c语言-表达式求值
  • 小型洗衣机哪个牌子质量好?口碑最好的四款小型洗衣机推荐