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

2023-12-23 16:28

本文主要是介绍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/528661

相关文章

Java Lambda表达式的使用详解

《JavaLambda表达式的使用详解》:本文主要介绍JavaLambda表达式的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言二、Lambda表达式概述1. 什么是Lambda表达式?三、Lambda表达式的语法规则1. 无参数的Lambda表

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Spring @RequestMapping 注解及使用技巧详解

《Spring@RequestMapping注解及使用技巧详解》@RequestMapping是SpringMVC中定义请求映射规则的核心注解,用于将HTTP请求映射到Controller处理方法... 目录一、核心作用二、关键参数说明三、快捷组合注解四、动态路径参数(@PathVariable)五、匹配请

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Java中的record使用详解

《Java中的record使用详解》record是Java14引入的一种新语法(在Java16中成为正式功能),用于定义不可变的数据类,这篇文章给大家介绍Java中的record相关知识,感兴趣的朋友... 目录1. 什么是 record?2. 基本语法3. record 的核心特性4. 使用场景5. 自定

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=