java - 在 Android 中使用 ORMLite 持久化 ArrayList<String>

标签 java android collections arraylist ormlite

当尝试使用 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;
    }
...

}

最佳答案

I have found a similar question Persisting a Collection class with ORMLite in android, but it seems not pointing the way to solve the problem.

不幸的是,这是 ORMLite 必须存储相关项集合的唯一方法。在您的情况下,您需要一个 Component 类,它具有如下内容:

公共(public)类组件{ @DatabaseField(外国=真) private Goods 商品; ...

然后您的 Goods 类将是:

public class Goods {
    @ForeignCollectionField
    private Collection<Component> components;
    ...

要将组件分配给 Goods 表中的条目,您可以创建一个 Component 条目并设置 goods 字段,并且 dao.create(...) 它。

看看 foreign-collection docs这可能会提供更多信息。 引用他们的话:

A foreign collection allows you to add a collection of orders on the account table. Whenever an Account object is returned by a query or refreshed by the DAO, a separate query is made over the order table and a collection of orders is set on the account. All of the orders in the collection have a corresponding foreign object that matches the account. For example:

public class Account {
    …
    @ForeignCollectionField(eager = false)
    ForeignCollection<Order> orders;
    …
}

In the above example, the @ForeignCollectionField annotation marks that the orders field is a collection of the orders that match the account. The field type of orders must be either ForeignCollection or Collection – no other collections are supported because they are much heavier with many methods to support.

关于java - 在 Android 中使用 ORMLite 持久化 ArrayList<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24350042/

相关文章:

java - Camel Rest DSL 响应编码

java - 带有 Selenium 和 Java 关键字的机器人框架

android - 尽管我已经从 Android Manifest 中删除了权限,但仍然在 Google Play 商店收到警告,该应用程序将被删除

java - 泛型中原始类型和 <?> 的区别

c# - LINQ 在两次实现 IEnumerable<T> 时感到困惑

java - 当使用相应的值代替时如何获取 key ?

java - 如何使用密码保护已经存在的 PDF?

Java Faster Equals 方法用于浮点类型

android - 在 Android 支持库中自定义导航 View

Android ActionBar 兼容溢出菜单未在 sdk 10 上显示