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

2023-12-26 16:45

本文主要是介绍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因为这个是支持  兼容安卓虚拟机。我们主推的方式

这篇关于Android 使用Serialiable接口和Parcelable接口进行数据传送的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/540159

相关文章

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例