e87
e87

Reputation: 1

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. 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.

   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);
        }
    });

Upvotes: 0

Views: 125

Answers (3)

ferini
ferini

Reputation: 1958

In your Modelo class, you are having fields like CheckBox and TextView. Those are framework classes that have a lot of internal state and are not just easily serializable to json.

You should only try to serialize the state, eg. text (String) of the TextView or boolean state of the CheckBox. 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.

Upvotes: 0

Gagan Batra
Gagan Batra

Reputation: 393

You are using mLista before initialising the variable. 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

Upvotes: 0

minchaej
minchaej

Reputation: 1834

I see your problem.

You cannot use Gson to Serialize UI-related classes such as CheckBox, Textview, Webview, etc..

Your Modelo class should only have String nombre and int image as Serializeable by removing UI-related classes such as CheckBox edad.

For it to be Seralizable by Gson, the data has to be Json Friendly (ex. String, int, Float etc..)

May I ask, why do you want to serialize CheckBox? Feel free to ask more questions.

Upvotes: 1

Related Questions