I am using Retrofit and ActiveAndroid ORM in my application. I have the following Model class:
@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;
}
}
Now, I obtain the Formresources once while starting the application and save it. Then in another activity I use the saved formresources to populate a listview. This much works fine. Now, I want to access the nested formresources like this:
formresourcelist.get(position).getResources();
This always returns a blank list of List<FormResource>
. What should I do to properly save and retrieve this list? I need to maintain compatibility with Retrofit at the same time.
I think I found a workaround. I made the following changes in the Model Class:
@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;
}
}
Basically I am serializing the ArrayList and persisting it as a String in the DB. While saving a FormResource, I do the following:
formresourceObject.setResourcelist();
formresourceObject.save();