I am trying to integrate my android project with a SQLite database, but I'm pretty new to this database stuff so I use Sugar ORM to help me with the database side of the project.
The problem is one of my class model contains Lists:
public class MyObject extends SugarRecord implements Parcelable {
public String myString;
public List<String> myStringList;
public MyObject () { }
private MyObject (Parcel in) {
myString= in.readString();
myStringList= new ArrayList<>();
in.readList(myStringList, String.class.getClassLoader());
}
public static final Creator<MyObject> CREATOR = new Creator<MyObject>() {
public MyObjectcreateFromParcel(Parcel in) { return new MyObject(in); }
public MyObject[] newArray(int size) { return new MyObject[size]; }
};
@Override
public int describeContents() { return 0; }
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(myString);
dest.writeList(myStringList);
}
}
When I try to read my database I get this error:
Class cannot be read from Sqlite3 database. Please check the type of field myStringList(java.util.List)
I suppose the way the columns are created on my database don't support lists. Is there any workaround for this? How should I aproach this?
SugarORM doesn't support storing Array
or Lists
There are strategies that you can use to support this but will require you to introduce a dependency.
The basic idea is to convert the list of data into JSON
then store it as String
import com.google.gson.Gson;
public class MyObject extends SugarRecord implements Parcelable {
public String myString;
@Ignore //Sugar will ignore this field.
private List<String> myStringList; //Change to private (accessed via getter/setter method)
private String myStringListStore; //to store the list in JSON format.
public MyObject () { }
private MyObject (Parcel in) {
this.myString= in.readString();
this.myStringList= new ArrayList<>();
in.readList(myStringList, String.class.getClassLoader());
}
public static final Creator<MyObject> CREATOR = new Creator<MyObject>(){
public MyObjectcreateFromParcel(Parcel in) { return new MyObject(in); }
public MyObject[] newArray(int size) { return new MyObject[size]; }
};
public List<String> getMyStringList(){
//Convert from JSON string to List
myStringList = new Gson().fromJson(this.myStringListStore,new TypeToken<List<String>>(){}.getType());
return myStringList;
}
public setMyStringList(List<String> stringList){
this.myStringList = stringList;
}
//Override save to ensure the list is converted into JSON before saving.
@Override
public long save(){
this.myStringListStore = new Gson().toJson(stringList);
return super.save();
}
@Override
public int describeContents() { return 0; }
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(myString);
dest.writeList(getMyStringList());
}
}