2017-01-23 53 views
0

我想將我的android項目與SQLite數據庫集成,但我對這個數據庫的東西很新,所以我使用Sugar ORM來幫助我處理項目的數據庫端。Android Sugar ORM - 帶列表的類模型

問題是我的類模型的一個包含列表:

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

當我嘗試閱讀我的數據庫中,我得到這個錯誤:

Class cannot be read from Sqlite3 database. Please check the type of field myStringList(java.util.List)

我想列創建的道路上我的數據庫不支持列表。有沒有解決方法?我應該如何解決這個問題?

回答

2

SugarORM不支持存儲ArrayLists 有,你可以用它來支持這一點,但會要求你介紹一個依賴策略。

的基本思想是將數據的列表轉換爲JSON然後存儲它作爲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()); 
    } 
} 
0

Class cannot be read from Sqlite3 database. Please check the type of field myStringList(java.util.List)

行:

myStringList= new ArrayList<>(); 

應該是:

myStringList= new ArrayList<String>(); 
0

什麼@Seth Kigen做,但而不是覆蓋save()方法中,我使用的setter類似/消氣劑myStringList存儲爲一個字符串:

@CompileStatic 
class MyObject extends SugarRecord { 

    @Ignore 
    private List<String> myStringList 

    // do not use this field 
    String myStringListStore 

    List<String> getMyStringList() { 
     return myStringListStore.split(',').toList() 
    } 

    void setMyStringList(List<String> a) { 
     this.myStringListStore = a.toString() 
    } 
} 

這是Groovy代碼,但Java代碼不會有很大的不同,用法:

MyObject b1 = new MyObject() 
    b1.myStringList = ['Lars Vogella','Subramanian'] 
    if (b1.save()){ 
     Log.d('DEBUG', "'${b1}' Saved.") 
    } 

這應該適用於一個簡單的用法(我用Groovy測試過),對於更復雜的List,您可能需要將myStringList轉換爲JsonObject並根據需要更新setter/getter。 希望這有助於。