美文网首页
Android Parceling an object with

Android Parceling an object with

作者: WotYang | 来源:发表于2018-05-23 00:33 被阅读0次
    1、场景:
    • Bundle
    • RoomDatabase
    2、注意事项:
    • write 和 read 的顺序一定要对应
    3、代码如下
    public class Note implements Parcelable {
    
        private int noteId;
    
        private JSONArray images;
    
        public int getNoteId() {
            return noteId;
        }
    
        public JSONArray getImages() {
            return images;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int i) {
            dest.writeInt(noteId);
            dest.writeString(this.images != null ? this.images.toString() : "[]");
        }
    
        protected Note(Parcel in) {
            noteId = in.readInt();
            String tempImages = in.readString();
            try {
                images = new JSONArray(tempImages != null ? tempImages :"[]");
            } catch (JSONException e) {
                images = new JSONArray();
            }
        }
    
        public static final Creator<Note> CREATOR = new Creator<Note>() {
            @Override
            public Note createFromParcel(Parcel in) {
                return new Note(in);
            }
    
            @Override
            public Note[] newArray(int size) {
                return new Note[size];
            }
        };
    }
    

    相关文章

      网友评论

          本文标题:Android Parceling an object with

          本文链接:https://www.haomeiwen.com/subject/wbdljftx.html