【发布时间】:2015-01-26 10:13:07
【问题描述】:
我曾经在我的项目中使用 ActiveRecord 库。当我的模型包含几个字段时,所有字段都正确保存。但如果我的模型包含其他模型,我无法保存。详情如下:
我的模型(正常工作):
@Table(name="Weathers")
public class WeatherResponse extends Model {
@Expose
@Column(name="code")
@SerializedName("cod")
public int cod;
@Expose
@Column(name="base")
@SerializedName("base")
public String base;
public WeatherResponse() {
super();
}
}
但如果我添加一个字段:
@Expose
@Column(name = "sys")
@SerializedName("sys")
public Sys sys;
然后这样做:
@Table(name="Weathers")
public class WeatherResponse extends Model {
@Expose
@Column(name="code")
@SerializedName("cod")
public int cod;
@Expose
@Column(name="base")
@SerializedName("base")
public String base;
@Expose
@Column(name = "sys")
@SerializedName("sys")
public Sys sys;
public WeatherResponse() {
super();
}
}
它的工作原理是保守的。我的班级:
@Table(name = "Sys")
public class Sys extends Model {
@Expose
@Column(name = "message")
@SerializedName("message")
public String message;
@Expose
@Column(name = "country")
@SerializedName("country")
public String country;
@Expose
@Column(name = "sunrise")
@SerializedName("sunrise")
public String sunrise;
@Expose
@Column(name = "sunset")
@SerializedName("sunset")
public String sunset;
public Sys() {
super();
}
}
我找到了一个示例,其中模型包含一系列其他模型。从这里我举个例子。enter link description here
但我不明白如何保存。
public void success(WeatherResponse weatherResponse, Response response) {
weatherResponse.save();
weatherResponse.sys.save();
}
【问题讨论】:
-
首先,保存你的 sys 对象,然后是其余的
标签: android activerecord android-sqlite