gpt4 book ai didi

java - 使用 gson 将自定义数组列表保存到 Sharedpreferences

转载 作者:行者123 更新时间:2023-11-29 22:51:52 24 4
gpt4 key购买 nike

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

有人知道最简单的方法是什么吗?如果它以任何方式提供帮助,则该对象本身是可以分析的。

P. S. 保存到共享首选项的最佳时间是什么时候 - 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;
}}

最佳答案

使用 Gson 库并将数组列表保存为字符串。下面的 fragment 保存为文件,但您也可以在 sharedpreference 中使用它:

public static void saveGroupChatFile(File file, List<GCRoom> list) throws IOException {
String data = new Gson().toJson(list);
FileOutputStream fout = new FileOutputStream(file, false);
OutputStreamWriter osw = new OutputStreamWriter(fout);
osw.write(data);
osw.close();
}

public static List<GCRoom> readGroupChatFile(File file) throws IOException {
Type listType = new TypeToken<List<GCRoom>>() {
}.getType();
JsonReader reader = new JsonReader(new FileReader(file));

return new Gson().fromJson(reader, listType);
}

至于图书馆:

implementation 'com.google.code.gson:gson:2.8.5'

关于java - 使用 gson 将自定义数组列表保存到 Sharedpreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57818480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com