android中自动生成parcelable

本文介绍了如何在Android Studio中利用插件自动生成Parcelable代码,包括安装插件的步骤和生成代码的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值