如何在ormlite数据库中存储arraylist

时间:2013-08-26 10:08:14

标签: android database

我正在尝试将类对象的arraylist保存到ormlite数据库中,但是它给出了错误java.lang.IllegalArgumentException:没有字段在类java.util.ArrayList中有DatabaseField注释

我的代码是

@DatabaseTable
public class ManageModelDetails {

@DatabaseField(generatedId = true)
private int id;

@DatabaseField(foreign = true, foreignAutoRefresh = true)
private ArrayList<ModelDetails> listModelDetails;

// ===============================================
public ManageModelDetails() {
    super();
}

// ===============================================

public ManageModelDetails(int id, ArrayList<ModelDetails> listModelDetails) {
    super();
    this.id = id;
    this.listModelDetails = listModelDetails;
}

// ===============================================

public void setId(int id) {
    this.id = id;
}

public int getId() {
    return id;
}

public void setModelList(ArrayList<ModelDetails> listModelDetails) {
    this.listModelDetails = listModelDetails;
}

public ArrayList<ModelDetails> getModelList() {
    return listModelDetails;
}

}

2 个答案:

答案 0 :(得分:6)

我认为你需要使用外国收藏。看看这里:

答案 1 :(得分:3)

如果要将对象的ArrayList保存为ORMLite,最简单的方法是:

@DatabaseField(dataType = DataType.SERIALIZABLE)
private SerializedList<MyObject> myObjects;

并获取我的对象列表:

public List<MyObject> getMyObjects() {
  return myObjects;
}

这里是SerializedList:

public class SerializedList<E> extends ArrayList<E> implements Serializable {
}