[英]i can't serialize my Arraylist to JSON with gson
I am trying to pass my array of objects to JSON, but when I do it the application crashes without any message in the log.我正在尝试将我的对象数组传递给 JSON,但是当我这样做时,应用程序崩溃而日志中没有任何消息。 Below I put the code, I am quite a newbie and it sure is a silly mistake.
下面我放了代码,我是一个新手,这肯定是一个愚蠢的错误。 Thanks in advance.
提前致谢。
public class Modelo implements Serializable {
@SerializedName("nombre")
private String nombre;
@SerializedName("edad")
private CheckBox edad;
@SerializedName("image")
private int image;
@SerializedName("txt")
private TextView txt;
public TextView getTxt() {
return txt;
}
public void setTxt(TextView txt) {
this.txt = txt;
}
public Modelo() {
}
public Modelo(String nombre, CheckBox edad, int image) {
this.nombre = nombre;
this.edad = edad;
this.image = image;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public CheckBox getEdad() {
return edad;
}
public void setEdad(CheckBox edad) {
this.edad = edad;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
Here the methods to pass the array of objects to JSON and another one to retrieve it.这里将对象数组传递给 JSON 和另一个检索它的方法。
public <T> void setList(String key, List<Modelo> list) {
Gson gson = new Gson();
String json = gson.toJson(list);
set(key, json);
}
public void set(String key, String value) {
SharedPreferences sharedPreferences =getApplicationContext().getSharedPreferences("list", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public List<Modelo> getList(){
SharedPreferences sharedPreferences =getApplicationContext().getSharedPreferences("list", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
List<Modelo> arrayItems=new ArrayList<>();
String serializedObject = sharedPreferences.getString("key", null);
if (serializedObject != null) {
Gson gson = new Gson();
Type type = new TypeToken<List<Modelo>>(){}.getType();
arrayItems = gson.fromJson(serializedObject, type);
}
return arrayItems;
}
And here is the part where I call the methods and where it crashes me.这是我调用方法的部分,它让我崩溃。 If necessary I put the entire code, I did not want to bore you.
如果有必要我把整个代码,我不想让你厌烦。
mAdapter = new ListAdapter(this, R.layout.item_row, mLista);
List<Modelo> mLista=new ArrayList<>();
myListView.setAdapter(mAdapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setList("key",mLista);
}
});
I see your problem.我看到了你的问题。
You cannot use Gson to Serialize UI-related classes such as CheckBox, Textview, Webview, etc..您不能使用 Gson 来序列化 UI 相关的类,例如 CheckBox、Textview、Webview 等。
Your Modelo class should only have String nombre
and int image
as Serializeable by removing UI-related classes such as CheckBox edad
.您的 Modelo class 应该只通过删除与 UI 相关的类(例如
CheckBox edad
)将String nombre
和int image
作为 Serializeable 。
For it to be Seralizable by Gson, the data has to be Json Friendly (ex. String, int, Float etc..)要使其可被 Gson 序列化,数据必须是 Json 友好的(例如字符串、整数、浮点数等。)
May I ask, why do you want to serialize CheckBox?请问,为什么要序列化CheckBox? Feel free to ask more questions.
随时提出更多问题。
You are using mLista
before initialising the variable.您在初始化变量之前使用
mLista
。 Try this:尝试这个:
List<Modelo> mLista=new ArrayList<>();
mAdapter = new ListAdapter(this, R.layout.item_row, mLista);
There is another issue with this.这还有另一个问题。 Your model class
Modelo
contains UI widget CheckBox
which cannot be serialised by Gson您的 model class
Modelo
包含无法由 Gson 序列化的 UI 小部件CheckBox
In your Modelo
class, you are having fields like CheckBox
and TextView
.在您的
Modelo
class 中,您拥有CheckBox
和TextView
等字段。 Those are framework classes that have a lot of internal state and are not just easily serializable to json.这些是具有大量内部 state 的框架类,并且不仅可以轻松地序列化为 json。
You should only try to serialize the state, eg.您应该只尝试序列化 state,例如。 text (String) of the TextView or boolean state of the CheckBox.
CheckBox 的 TextView 或 boolean state 的文本(字符串)。 Later, when you read the state from json, you can setup your UI based on the stored values, no need to store whole UI components.
稍后,当您从 json 读取 state 时,您可以根据存储的值设置您的 UI,无需存储整个 UI 组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.