使用ActiveAndroid ORM和Retrofit

时间:2016-07-25 03:29:43

标签: java android retrofit retrofit2 activeandroid

我在我的应用程序中使用Retrofit和ActiveAndroid ORM。我有以下Model类:

@Table(name = "formresource")
public class FormResource extends Model implements Serializable{

    @Column(name="name")
    @SerializedName("name")
    @Expose
    private String name;

    @Column
    @SerializedName("resources")
    @Expose
    private List<FormResource> resources = new ArrayList<FormResource>();

    @Column(name = "valueReference")
    @SerializedName("valueReference")
    @Expose
    private String valueReference;


    @Column(name = "uuid")
    @SerializedName("uuid")
    @Expose
    private String uuid;

    @Column(name = "display")
    @SerializedName("display")
    @Expose
    private String display;

    @Column(name = "links")
    @SerializedName("links")
    @Expose
    private List<Link> links = new ArrayList<Link>();

    public FormResource()
    {
        super();
    }

    public String getUuid() {
        return uuid;
    }


    public void setUuid(String uuid) {
        this.uuid = uuid;
    }


    public String getDisplay() {
        return display;
    }


    public void setDisplay(String display) {
        this.display = display;
    }


    public List<Link> getLinks() {
        return links;
    }


    public void setLinks(List<Link> links) {
        this.links = links;
    }

    public String getValueReference() {
        return valueReference;
    }

    public void setValueReference(String valueReference) {
        this.valueReference = valueReference;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<FormResource> getResources() {
        return resources;
    }

    public void setResources(List<FormResource> resources) {
        this.resources = resources;
    }

}

现在,我在启动应用程序时获取Formresources并保存一次。然后在另一个活动中,我使用保存的formresources来填充listview。这很好用。现在,我想访问嵌套的formresources,如下所示:

formresourcelist.get(position).getResources();

这始终会返回List<FormResource>的空白列表。我该怎么做才能正确保存和检索此列表?我需要同时保持与Retrofit的兼容性。

2 个答案:

答案 0 :(得分:0)

由于您使用Retrofit填充FormResource数据,因此不应初始化模型中的任何字段。

这一行是问题所在:

private List<FormResource> resources = new ArrayList<FormResource>();

尝试删除初始化并只声明如下字段:

private List<FormResource> resources;

然后尝试拨打formresourcelist.get(position).getResources();

祝你好运!

答案 1 :(得分:0)

我想我找到了一个解决方法。我在模型类中进行了以下更改:

@Table(name = "formresource")
public class FormResource extends Model implements Serializable{

    Gson gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    Type formresourcelistType = new TypeToken<List<FormResource>>(){}.getType();


    @SerializedName("resources")
    @Expose
    private List<FormResource> resources = new ArrayList<FormResource>();

    @Column(name = "resources")
    @Expose
    private String resourcelist;


    public List<FormResource> getResources() {
        return resources;
    }

    public void setResources(List<FormResource> resources) {
        this.resources = resources;
    }

    public void setResourcelist()
    {
        this.resourcelist=gson.toJson(resources,formresourcelistType);
    }

    public List<FormResource> getResourceList() {

        List<FormResource> resourceList=gson.fromJson(this.resourcelist,formresourcelistType);
        return resourceList;
    }

}

基本上我正在序列化ArrayList并将其作为字符串保存在DB中。保存FormResource时,我执行以下操作:

formresourceObject.setResourcelist();
formresourceObject.save();