【问题标题】:Persisting ArrayList<String> with ORMLite in Android在 Android 中使用 ORMLite 持久化 ArrayList<String>
【发布时间】:2014-08-12 13:20:22
【问题描述】:
当尝试使用 ORMLite 持久化 Goods 类时,我不知道如何在这里持久化 componentList。我发现了一个类似的问题Persisting a Collection class with ORMLite in android,但似乎没有指出解决问题的方法。
public class Goods extends Object implements Serializable
{
private static final long serialVersionUID = -7560219283718464481L;
...
public ArrayList<String> componentList;
public String getComponentList(){
return componentList;
}
public void setComponentList(ArrayList<String> componentList){
this.componentList = componentList;
}
...
}
【问题讨论】:
标签:
java
android
collections
arraylist
ormlite
【解决方案1】:
我发现了一个类似的问题 Persisting a Collection class with ORMLite in android,但似乎没有指出解决问题的方法。
不幸的是,这是 ORMLite 存储相关项目集合的唯一方法。在您的情况下,您需要一个 Component 类,该类具有以下内容:
公共类组件{
@DatabaseField(外=真)
私人物品;
...
然后您的Goods 课程将是:
public class Goods {
@ForeignCollectionField
private Collection<Component> components;
...
要将组件分配给Goods 表中的条目,您将创建一个Component 条目并设置goods 字段和dao.create(...)。
查看foreign-collection docs,它可能会提供更多信息。
引用他们的话:
外部集合允许您在帐户表中添加订单集合。每当查询返回 Account 对象或由 DAO 刷新时,都会对订单表进行单独的查询,并在帐户上设置订单集合。集合中的所有订单都有一个与帐户匹配的对应外来对象。例如:
public class Account {
…
@ForeignCollectionField(eager = false)
ForeignCollection<Order> orders;
…
}
在上面的示例中,@ForeignCollectionField 注释标记了 orders 字段是与帐户匹配的订单的集合。订单的字段类型必须是 ForeignCollection 或 Collection - 不支持其他集合,因为它们要重得多,支持的方法很多。