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

济南招考院网站今日新闻头条新闻今天

济南招考院网站,今日新闻头条新闻今天,做留言的网站,云部署Wordpress一、前言 这篇文章主要针对Serialiable和Parcelable接口来传递对象。呈现的功能是跳转到另一个界面,然后通过toast展现我收到的数据。 二、使用Serialiable接口传递数据 1.创建需要传递的对象 //必须实现Serializable接口,此对象才有传递的资格 publ…

一、前言

这篇文章主要针对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.hkea.cn/news/8157/

相关文章:

  • 网站建设的要求有哪些广告推广赚钱
  • 网站建设价格在哪济南兴田德润优惠吗深圳百度竞价推广
  • 河南企业网官方网站河南推广网站的公司
  • 做网站的编程语言免费网站建设模板
  • 做网站友汇网网站优化建议
  • 企业网站备案域名可以用个人的核心关键词和长尾关键词举例
  • 上海做网页公司莆田seo推广公司
  • 少儿编程自学网站企业网站建设规划
  • 厦门网站建设培训学校网站制作基本流程
  • 中国扬州 政府门户网站建设方案怎么做网络销售
  • 现在的网站前端用什么做青岛关键词优化seo
  • 公司网站海报怎么做计算机基础培训机构
  • 什么行业 网站seo网络推广到底是做什么的
  • 东城网站建设百度网盘官网网页版
  • 兰溪市网站建设公司上海广告公司
  • 淘宝单页面网站semir
  • 广州动态网站开发百度百家官网入口
  • 网站设计部的优化google官网下载安装
  • 海口今天发现一例抖音seo公司
  • 鞍山网站seo的实现方式
  • phpcms 后台修改修改网站备案号南京网站设计优化公司
  • 福布斯中国100名人榜seo网络推广优势
  • 东台专业做网站十大场景营销案例
  • 网站建设建网站网络推广方式有哪几种
  • 网站模板源代码下载什么是搜索引擎竞价推广
  • phpcmsv9手机网站模板西安网站建设公司
  • 如何选择个人网站主题软件开发培训中心
  • 拓者设计吧室内设计官网案例宁波谷歌seo
  • wordpress全屏背景信息流优化师职业规划
  • 浙江网站建设企业优化大师官方正版下载