告别手写parcelable
来源:互联网 发布:set java 编辑:程序博客网 时间:2024/05/05 02:46
在eclipse中
- 推荐parcelable code generator
用法
- 下载该开源项目
- 导入到IDE
- 参照下载下来的实例编写json文件来描述目标类
- 运行该java工程
- 生成的目标类在output文件夹
- 拷贝生成的类到需要它的工程(注意:这整个使用过程没有接下来在android studio中的方法简单)
在android studio中
- 推荐安装插件, android parcelable code generator
用法
- 书写自己的目标类
public class DemoParcelable { String aString; int aInt; double aDouble; HashMap<String, String> aHashMap; ArrayList<String> aArrayList; DemoAnotherClass anotherClass; class DemoAnotherClass{ ConcurrentHashMap<String, String> aConcurrentHashMap; }}
- 在类名处
alt + insert
,在弹出界面选择Parcelable,最后生成的代码如下
public class DemoParcelable implements Parcelable { public static final Parcelable.Creator<DemoParcelable> CREATOR = new Parcelable.Creator<DemoParcelable>() { public DemoParcelable createFromParcel(Parcel source) { return new DemoParcelable(source); } public DemoParcelable[] newArray(int size) { return new DemoParcelable[size]; } }; String aString; int aInt; double aDouble; HashMap<String, String> aHashMap; ArrayList<String> aArrayList; DemoAnotherClass anotherClass; public DemoParcelable() { } protected DemoParcelable(Parcel in) { this.aString = in.readString(); this.aInt = in.readInt(); this.aDouble = in.readDouble(); this.aHashMap = (HashMap<String, String>) in.readSerializable(); this.aArrayList = in.createStringArrayList(); this.anotherClass = in.readParcelable(DemoAnotherClass.class.getClassLoader()); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.aString); dest.writeInt(this.aInt); dest.writeDouble(this.aDouble); dest.writeSerializable(this.aHashMap); dest.writeStringList(this.aArrayList); dest.writeParcelable(this.anotherClass, flags); } static class DemoAnotherClass implements Parcelable { public static final Creator<DemoAnotherClass> CREATOR = new Creator<DemoAnotherClass>() { public DemoAnotherClass createFromParcel(Parcel source) { return new DemoAnotherClass(source); } public DemoAnotherClass[] newArray(int size) { return new DemoAnotherClass[size]; } }; ConcurrentHashMap<String, String> aConcurrentHashMap; public DemoAnotherClass() { } protected DemoAnotherClass(Parcel in) { this.aConcurrentHashMap = (ConcurrentHashMap<String, String>) in.readSerializable(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeSerializable(this.aConcurrentHashMap); } }}
4 0
- 告别手写parcelable
- 告别手写parcelable
- android IPC : 告别手写parcelable
- 一行代码搞定Parcelable 告别Parcelable繁琐代码!
- 一行代码搞定Parcelable,告别Parcelable繁琐代码!
- 一行代码搞定Parcelable 告别Parcelable繁琐代码!
- 告别手写 API文档生成工具推荐
- 告别手写 API文档生成工具推荐
- 告别手写 API文档生成工具推荐
- 告别手写 API文档生成工具推荐
- 告别手写 API文档生成工具推荐
- 告别手写 API文档生成工具推荐
- 告别手写 API文档生成工具推荐
- 告别手写 API文档生成工具推荐
- 告别
- 告别
- 告别
- 告别
- Java分割字符串函数split的用法
- 使用mysql++ 的一些api
- win7 usb 无法识别
- 面试日记
- C语言一些细节
- 告别手写parcelable
- 遍歷數組
- nyoj 206 矩形的个数 【找规律】
- MVC+EF实现分页代码
- LeetCode221:Maximal Square
- HDU 1671 Phone List 字典树
- hdu 1874
- SpringMVC 异常处理
- 读设计师要懂心理学笔记7-人是社会性动物