android – 自定义对象的ArrayList没有保存到GSON的SharedPreferences

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

我已经尝试过使用GSON,但我认为有些东西是我遗漏的,我看不到.字符串我的对象被转换为已正确保存但没有任何内容存储在其中.

我正在使用此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始终是空的,当我在我的文件浏览器中检查时,首选项是空的,如此截图android  – 自定义对象的ArrayList没有保存到GSON的SharedPreferences

还有这个

android  – 自定义对象的ArrayList没有保存到GSON的SharedPreferences

我的POJO既可以Parcelable也可以序列化.我能做错什么?我会发布任何所需的代码.谢谢.

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

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并直接将项添加到列表中,如下所示

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 – 使用复杂的JSON进行Retrofit POST请求


下一篇:spring依赖注入