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

相关文章

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

mysql通过frm和ibd文件恢复表_mysql5.7根据.frm和.ibd文件恢复表结构和数据

《mysql通过frm和ibd文件恢复表_mysql5.7根据.frm和.ibd文件恢复表结构和数据》文章主要介绍了如何从.frm和.ibd文件恢复MySQLInnoDB表结构和数据,需要的朋友可以参... 目录一、恢复表结构二、恢复表数据补充方法一、恢复表结构(从 .frm 文件)方法 1:使用 mysq

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

golang获取prometheus数据(prometheus/client_golang包)

《golang获取prometheus数据(prometheus/client_golang包)》本文主要介绍了使用Go语言的prometheus/client_golang包来获取Prometheu... 目录1. 创建链接1.1 语法1.2 完整示例2. 简单查询2.1 语法2.2 完整示例3. 范围值

Python中conda虚拟环境创建及使用小结

《Python中conda虚拟环境创建及使用小结》本文主要介绍了Python中conda虚拟环境创建及使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录0.前言1.Miniconda安装2.conda本地基本操作3.创建conda虚拟环境4.激活c

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解