Android Parceling an object with
2018-05-23 本文已影响0人
WotYang
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];
}
};
}