首页 > 解决方案 > 使用 gson 将自定义数组列表保存到共享首选项

问题描述

我正在尝试将我的应用程序状态保存到共享首选项。我要保存的信息是自定义对象的数组列表,其中每个对象(PatientInfo)包含一些字符串和另外 2 个自定义数组列表(SkinPhotoInfo、TreatmentsInfo)。我能够保存和加载自定义对象的数组列表,但我无法保存其中包含数组列表的数组列表。

任何人都知道最简单的方法是什么?如果它以任何方式提供帮助,对象本身就已经可以分析了。

PS 何时是保存到共享首选项的最佳时间 - onPause 或 onDelete?

感谢您的帮助!!

患者信息:

public class PatientInfo implements Parcelable {

String name;
String skinType;
String notes;
String image;
ArrayList<SkinPhotoInfo> skinPhotos;
ArrayList<TreatmentsInfo> treatments;
Boolean showDeleteButton;

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(skinType);
    dest.writeString(notes);
    dest.writeValue(image);
    dest.writeValue(skinPhotos);
    dest.writeValue(treatments);
}

public static final Creator<PatientInfo> CREATOR = new Creator<PatientInfo>()
{
    @Override
    public PatientInfo createFromParcel(Parcel source) {
        PatientInfo ret = new PatientInfo();
        ret.name = source.readString();
        ret.skinType = source.readString();
        ret.notes = source.readString();
        ret.image = (String)source.readString();
        ret.skinPhotos = source.readArrayList(null);
        ret.treatments = source.readArrayList(null);

        return ret;
    }

    @Override
    public PatientInfo[] newArray(int size) {
        return new PatientInfo[size];
    }
};

public PatientInfo() {
    this.name = "";
    this.skinType = "";
    this.image = "";
    this.skinPhotos = new ArrayList<SkinPhotoInfo>();
    this.showDeleteButton = false;
    this.treatments = new ArrayList<TreatmentsInfo>();
}}

皮肤照片信息:

public class SkinPhotoInfo implements Parcelable {

String photoDate;
Boolean showDeleteButton;
Uri imageUri;

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(photoDate);
    dest.writeByte((byte)(showDeleteButton ? 1 : 0)); // If showDeleteButton == true, byte == 1
    dest.writeValue(imageUri);
}

public static final Creator<SkinPhotoInfo> CREATOR = new Creator<SkinPhotoInfo>()
{
    @Override
    public SkinPhotoInfo createFromParcel(Parcel source) {
        SkinPhotoInfo ret = new SkinPhotoInfo();
      ret.skinImageThumnail = (Bitmap)source.readValue(Bitmap.class.getClassLoader());
        ret.photoDate = source.readString();
        ret.showDeleteButton = source.readByte() != 1;
        ret.imageUri = (Uri) source.readValue(Uri.class.getClassLoader());
        return ret;
    }

    @Override
    public SkinPhotoInfo[] newArray(int size) {
        return new SkinPhotoInfo[size];
    }
};

    public SkinPhotoInfo(Uri imageUri, String photoDate) {
    this.imageUri = imageUri;
    this.photoDate = photoDate;
    showDeleteButton = false;
}}

治疗信息:

public class TreatmentsInfo implements Parcelable {
String treatmentDate;
String treatmentName;
String pattern = "MM-dd-yy";
Boolean showDeleteButton;

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(treatmentDate);
    dest.writeString(treatmentName);
    dest.writeString(pattern);
    dest.writeByte((byte)(showDeleteButton ? 1 : 0)); // If showDeleteButton == true, byte == 1
}

public static final Creator<TreatmentsInfo> CREATOR = new Creator<TreatmentsInfo>()
{
    @Override
    public TreatmentsInfo createFromParcel(Parcel source) {
        TreatmentsInfo ret = new TreatmentsInfo();
        ret.treatmentDate = source.readString();
        ret.treatmentName = source.readString();
        ret.pattern = source.readString();
        ret.showDeleteButton = source.readByte() != 1;
        return ret;
    }

    @Override
    public TreatmentsInfo[] newArray(int size) {
        return new TreatmentsInfo[size];
    }
};

public TreatmentsInfo(){
    this.treatmentDate = "";
    this.treatmentName = "";
    this.showDeleteButton = false;
    this.pattern = "";
}

public TreatmentsInfo(String treatmentDate, String treatmentName) {
    this.treatmentDate = treatmentDate;
    this.treatmentName = treatmentName;
    this.showDeleteButton = false;
}}

标签: javaandroidgson

解决方案


您可以执行以下操作:

String json = new Gson().toJson(YourObject);

保存在共享首选项中。要检索 json 并将其转换为 YourObejct,只需执行以下操作:

String json = myPrefsObject.getString(TAG, "");    
return new Gson().fromJson(json, YourObject.class);

至于PS问题,答案是onPause。让我知道您是否需要其他东西


推荐阅读