gpt4 book ai didi

android - 自定义对象的 ArrayList 不使用 GSON 保存到 SharedPreferences

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:48 25 4
gpt4 key购买 nike

我有一个自定义 POJO 对象的 ArrayList,我想将其保存到 SharedPreferences,这样它们就不会被垃圾收集,用户应用程序关闭后仍然可以访问它们。

我尝试过使用 GSON,但我认为缺少一些我看不到的东西。String 我的 Object 是转换为已正确保存,但未存储任何内容。

我正在使用此Click 事件 来存储:

cartBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_SHORT).show();
SharedPreferences mPrefs = getSharedPreferences("Cart", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();

ArrayList<ItmModel> arrayList = new ArrayList<>();
AtomicReference<ItmModel> itmModel = new AtomicReference<>(new ItmModel(item.getText().toString(), attr.getText().toString(),
pric.getText().toString(), sise.getText().toString(), image, company));
arrayList.add(itmModel.get());

Gson gson = new Gson();
Type type = new TypeToken<ArrayList<ItmModel>>() {
}.getType();
String list = gson.toJson(arrayList, type);
prefsEditor.putString("Cart", list);
prefsEditor.apply();

}
});

我正在这样检索它:

SharedPreferences sharedPreferences = getSharedPreferences("Cart", MODE_PRIVATE);
Gson gson = new Gson();
String list = sharedPreferences.getString("Cart", "");
Type type = new TypeToken<ArrayList<ItmModel>>() {
}.getType();
arrayList = gson.fromJson(list, type);
if (arrayList == null) {
arrayList = new ArrayList<>();
}

final CrtAdapter crtAdapter = new CrtAdapter(arrayList);

RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setAdapter(crtAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
recyclerView.setLayoutManager(linearLayoutManager);
runLayoutAnimation(recyclerView);
recyclerView.setOnFlingListener(null);

尽管我做了所有尝试,RecyclerView 始终是空的,当我检查我的File explorer 时,Preference 是空的,就像这样 截图 Pic 1

还有这个

Pic 2

我的 POJOParcelableSerializable。我做错了什么?我会发布任何需要的代码。谢谢。

这是我从点击事件的日志中得到的。我看到对象已添加但后来从列表中删除。为什么会发生这种情况?

04-06 09:53:42.959 4078-4078/kamble.gml.woodleyint D/dalvikvm: create interp thread : stack size=128KB
04-06 09:53:42.959 4078-4078/kamble.gml.woodleyint D/dalvikvm: create new thread
04-06 09:53:42.959 4078-4078/kamble.gml.woodleyint D/dalvikvm: new thread created
04-06 09:53:42.959 4078-4078/kamble.gml.woodleyint D/dalvikvm: update thread list
04-06 09:53:42.959 4078-4455/kamble.gml.woodleyint D/dalvikvm: threadid=26: interp stack at 0x57bee000
04-06 09:53:42.959 4078-4455/kamble.gml.woodleyint D/dalvikvm: threadid=26: created from interp
**04-06 09:53:42.960 4078-4078/kamble.gml.woodleyint D/dalvikvm: threadid=26: adding to list**
04-06 09:53:42.960 4078-4078/kamble.gml.woodleyint D/dalvikvm: start new thread
04-06 09:53:42.960 4078-4455/kamble.gml.woodleyint D/dalvikvm: threadid=26: notify debugger
04-06 09:53:42.960 4078-4455/kamble.gml.woodleyint D/dalvikvm: threadid=26 (SharedPreferencesImpl-load): calling run()
04-06 09:53:42.968 4078-4455/kamble.gml.woodleyint D/dalvikvm: threadid=26: exiting
04-06 09:53:42.968 4078-4455/kamble.gml.woodleyint D/dalvikvm: threadid=26: detach (group=0x418d8ce0)
**04-06 09:53:42.968 4078-4455/kamble.gml.woodleyint D/dalvikvm: threadid=26: removing from list
04-06 09:53:42.968 4078-4455/kamble.gml.woodleyint D/dalvikvm: threadid=26: bye!
04-06 09:53:43.143 4078-4078/kamble.gml.woodleyint D/Cart: [[]]**
04-06 09:53:43.146 4078-4078/kamble.gml.woodleyint D/dalvikvm: create interp thread : stack size=128KB
04-06 09:53:43.146 4078-4078/kamble.gml.woodleyint D/dalvikvm: create new thread
04-06 09:53:43.146 4078-4078/kamble.gml.woodleyint D/dalvikvm: new thread created
04-06 09:53:43.146 4078-4078/kamble.gml.woodleyint D/dalvikvm: update thread list
04-06 09:53:43.146 4078-4456/kamble.gml.woodleyint D/dalvikvm: threadid=26: interp stack at 0x57bee000
04-06 09:53:43.146 4078-4456/kamble.gml.woodleyint D/dalvikvm: threadid=26: created from interp
**04-06 09:53:43.146 4078-4078/kamble.gml.woodleyint D/dalvikvm: threadid=26: adding to list**
04-06 09:53:43.146 4078-4078/kamble.gml.woodleyint D/dalvikvm: start new thread
04-06 09:53:43.152 4078-4456/kamble.gml.woodleyint D/dalvikvm: threadid=26: notify debugger
04-06 09:53:43.152 4078-4456/kamble.gml.woodleyint D/dalvikvm: threadid=26 (pool-3-thread-1): calling run()

这是我的POJO:

public class ItmModel extends ArrayList implements Parcelable, Serializable {
@SuppressWarnings("unused")
public static final Parcelable.Creator<ItmModel> CREATOR = new Parcelable.Creator<ItmModel>() {
@Override
public ItmModel createFromParcel(Parcel in) {
return new ItmModel(in);
}

@Override
public ItmModel[] newArray(int size) {
return new ItmModel[size];
}
};
private String name;
private String attr;
private String size;
private String pric;
private String imag;
private String comp;

public ItmModel(String name, String attr, String size, String pric, String imag, String comp) {
this.name = name;
this.attr = attr;
this.size = size;
this.pric = pric;
this.imag = imag;
this.comp = comp;
}

public ItmModel() {

}

private ItmModel(Parcel in) {
name = in.readString();
attr = in.readString();
size = in.readString();
pric = in.readString();
imag = in.readString();
comp = in.readString();
}

public String getName() {
return name;
}

public void setName(String companyName) {
this.comp = companyName;
}

public String getAttr() {
return attr;
}

public void setAttr(String attr) {
this.attr = attr;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}

public String getPric() {
return pric;
}

public void setPric(String pric) {
this.pric = pric;
}

public String getImag() {
return imag;
}

public void setImag(String imag) {
this.imag = imag;
}

public String getComp() {
return comp;
}

public void setComp(String comp) {
this.comp = comp;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(attr);
dest.writeString(size);
dest.writeString(pric);
dest.writeString(imag);
dest.writeString(comp);
}}

最佳答案

尝试删除AtomicReference 并直接将item 添加到list

ArrayList<ItmModel> arrayList = new ArrayList<>();
ItmModel itmModel = new ItmModel(item.getText().toString(), attr.getText().toString(),pric.getText().toString(), sise.getText().toString(), image, company);
arrayList.add(itmModel);

Gson gson = new Gson();
Type type = new TypeToken<ArrayList<ItmModel>>() {}.getType();
String list = gson.toJson(arrayList, type);
prefsEditor.putString("Cart", list);
prefsEditor.apply();

为什么你还要在 POJO 中扩展 ArrayList,删除并修改部分 POJO 到下面

public class ItmModel implements Parcelable, Serializable {
@SuppressWarnings("unused")
public static final Parcelable.Creator<ItmModel> CREATOR = new Parcelable.Creator<ItmModel>() {
@Override
public ItmModel createFromParcel(Parcel in) {
return new ItmModel(in);
}

@Override
public ItmModel[] newArray(int size) {
return new ItmModel[size];
}
};
@Expose
private String name;
@Expose
private String attr;
@Expose
private String size;
@Expose
private String pric;
@Expose
private String imag;
@Expose
private String comp;
//rest of code as it is
}

关于android - 自定义对象的 ArrayList 不使用 GSON 保存到 SharedPreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49686880/

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