[原创]如何在Parcelable中使用泛型

实体类在实现Parcelable接口时,除了要实现它的几个方法之外,还另外要定义一个静态常量CREATOR,如下例所示:

     public static class ProductModel implements Parcelable {
public String id;
public String title;
public String sold_num;
public String max_month;
public String cprice;
public double month_price;
public List<String> gpic; @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.title);
dest.writeString(this.sold_num);
dest.writeString(this.max_month);
dest.writeString(this.cprice);
dest.writeDouble(this.month_price);
dest.writeStringList(this.gpic);
} public ProductModel() {
} protected ProductModel(Parcel in) {
this.id = in.readString();
this.title = in.readString();
this.sold_num = in.readString();
this.max_month = in.readString();
this.cprice = in.readString();
this.month_price = in.readDouble();
this.gpic = in.createStringArrayList();
} public static final Creator<ProductModel> CREATOR = new Creator<ProductModel>() {
public ProductModel createFromParcel(Parcel source) {
return new ProductModel(source);
} public ProductModel[] newArray(int size) {
return new ProductModel[size];
}
};

CREATOR在这里成为了一个约定,而没有放到接口定义里面,个人感觉这样封装得不是很好,不知道是不是实在没有更好的解决办法才弄成这样的?

假如在类里要使用泛型,麻烦就来了,例如这样

 public class PageDataModel<T extends Parcelable> extends APIDataModel {

     static final String DATA_KEY = "data";

     /**
* count : 1
* total_pages : 1
* list_rows : 10
* first_row : 0
*/ public int count;
public ArrayList<T> data;
public int total_pages;
public int list_rows;
public int first_row;
}

这个data,就无法正常read(write倒是可以)。

正常的write方式是这样的:

this.data = dest.createTypedArrayList(Parcelable.Creator<T> c)

那么问题来了,你只有一个泛型T,没有具体类型,拿不到它的CREATOR!CREATOR只是一个约定,而且是跟具体类型绑定的。

所以我前面说如果这个CREATOR能通过某种方式定义下来,这里或许就能拿到了,然而并没有。

但是android系统你是改变不了的,我们仍然要解决问题。经过多番调查尝试之后,终于找到了解决办法:

Pacel是可以读取和写入Bundle对象的,而Bundle对象又可以读取和写入Parcelable,而且不需要CREATOR(为啥Parcel不能设计成这样,坑爹呢。。)

那么 我们把泛型数据用Bundle包装一下即可,下面是具体代码,亲测有效,总算解决这个问题了。

 //write
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(DATA_KEY, data);
dest.writeBundle(bundle); //read
this.data = in.readBundle().getParcelableArrayList(DATA_KEY);

[原创]如何在Parcelable中使用泛型的更多相关文章

  1. (原创)如何在spannableString中使用自定义字体

    最近在做车联网的产品,主打的是语音交互和导航功能,UI给的导航界面可真是够酷炫的.但麻烦的事情也来了,里面的一句话居然用到了三种字体.界面如图所示: 从图中可以看出 500m左前方行驶 居然使用了三种 ...

  2. 【原创】如何在Android中为TextView动态设置drawableLeft等

    如何在Android中为TextView动态设置drawableLeft等   两种方式:   方式1:手动设置固有边界 Drawable drawable = getResources().getD ...

  3. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  4. 如何在Dreamweaver中使用emmet

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=3666 一.emmet ...

  5. 如何在Android中使用OpenCV

    如何在Android中使用OpenCV 2011-09-21 10:22:35 标签:Android 移动开发 JNI OpenCV NDK 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...

  6. MVC如何在Pipeline中接管请求的?

    MVC如何在Pipeline中接管请求的? 文章内容 上个章节我们讲到了,可以在HttpModules初始化之前动态添加Route的方式来自定义自己的HttpHandler,最终接管请求的,那MVC是 ...

  7. 服务化改造实践 | 如何在 Dubbo 中支持 REST

    什么是 REST REST 是 Roy Thomas Fielding [[1]](#fn1) 在 2000 年他的博士论文 [[2]](#fn2) “架构风格以及基于网络的软件架构设计” 中提出来的 ...

  8. Nodejs的安装配置及如何在sublimetext2中运行js

    Nodejs的安装配置及如何在sublimetext2中运行js听语音 | 浏览:4554 | 更新:2015-06-16 11:29 Nodejs的安装配置及如何在sublimetext2中运行js ...

  9. Scala进阶之路-Scala中的泛型介绍

    Scala进阶之路-Scala中的泛型介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 通俗的讲,比如需要定义一个函数,函数的参数可以接受任意类型.我们不可能一一列举所有的参数类 ...

随机推荐

  1. LeetCode之171. Excel Sheet Column Number

    ---------------------------------- 乘权相加即可. AC代码:(从右往左) public class Solution { public int titleToNum ...

  2. 微信开发之Author网页授权

     微信开发中,经常有这样的需求:获得用户头像.绑定微信号给用户发信息.. 那么实现这些的前提就是授权!   1.配置安全回调域名: 在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的&q ...

  3. Knockout.js随手记(2)

    计算属性 konckout.js的API文档,写的极为详细和生动,透过MVVM的运作原理,开发时只需专注于定义ViewModel逻辑,不需耗费心力处理TextBox.Select的onchange.o ...

  4. [Liferay6.2.2]AUI的小坑:input的type属性

    <aui:input name="name" label="姓名" value="<%=student.getName() %>&q ...

  5. Quartz 之Quartz Cron表达式

    说到这个Quartz了,必不可少的就要说到我们的Triggger触发器,相信大家也都知道,我们在之前也说过了,Trigger又有两个子类,也就是两种方式,分别是:SimpleTrigger和CronT ...

  6. 《DSP using MATLAB》示例Example5.10

    代码: n = 0:10; x = 10*(0.8) .^ n; [xec, xoc] = circevod(x); %% -------------------------------------- ...

  7. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  8. 【转】java-String中的 intern()

    转自:http://blog.sina.com.cn/s/blog_69dcd5ed0101171h.html 1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值 ...

  9. Websocket简单例子

    websocket是Html5的一个协议,也就是说距离我们2016年就几年时间,其他原理我就不说了,直接讲例子 一.准备材料:1.一个开发工具必须支持javaEE7的,原因是javaEE6或以下不支持 ...

  10. Leetcode Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...