Storing List of objects using DataType.SERIALIZABLE in ormlite android

我们两清 提交于 2019-12-07 13:26:11

问题


How to save an ArrayList using ORMlite in android my model is as follows

class Model{
    @DatabaseField
    public String type = "";

    @DatabaseField
    public String name = "";

    @DatabaseField
    public Date dateTime = null;

    @DatabaseField
    ArrayList<Item> items = null;
}

And Item class has

class Item{
     @DatabaseField
     String itemName;
     ...
}

I am getting the following exception :

java.sql.SQLException: ORMLite can't store unknown class class
      java.util.ArrayList for field 'items'.   Serializable fields must specify
      dataType=DataType.SERIALIZABLE

But when i specify my field as

@DatabaseField(dataType = DataType.SERIALIZABLE)
ArrayList<Item> items = null;

The compiler gives a error called field cannot be resolved please help me with this.


回答1:


I've just changed the error message to be:

ORMLite does not know how to store class java.util.ArrayList for field 'items'. Use another class, custom persister, or to serialize it use dataType=DataType.SERIALIZABLE

Maybe that makes more sense? ORMLite is trying to say that it doesn't know how to store lists in a field. If you want to store collection of items then maybe you should take a look at the foreign-collection documentation.



来源:https://stackoverflow.com/questions/17691507/storing-list-of-objects-using-datatype-serializable-in-ormlite-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!