我无法处理ActiveRecord android
我曾经在我的项目中使用ActiveRecord库。当我的模型包含多个字段时,所有字段都会正确保存。但如果我的模型包含其他模型,则无法保存。详情如下: 我的modelso工作原理:我无法处理ActiveRecord android,android,activerecord,android-sqlite,Android,Activerecord,Android Sqlite,我曾经在我的项目中使用ActiveRecord库。当我的模型包含多个字段时,所有字段都会正确保存。但如果我的模型包含其他模型,则无法保存。详情如下: 我的modelso工作原理: @Table(name="Weathers") public class WeatherResponse extends Model { @Expose @Column(name="code") @SerializedName("cod") p
@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();
}
}
我发现了一个示例,其中模型包含一系列其他模型。从这里我举了一个例子
但如何在我的情况下保持我不明白
public void success(WeatherResponse weatherResponse, Response response) {
weatherResponse.save();
weatherResponse.sys.save();
}
根据此wiki,您需要将WeatherResponse的sys字段分配给另一个对象,如下所示:
Sys sys = new Sys();
sys.message = "something";
//another assignments
sys.save();
weatherResponse.sys = sys;
weatherResponse.save();
关键是,我的对象是使用JSON生成的,然后您需要解析JSON,从中创建Sys对象,并将该对象分配给weatherResponse.Sys字段,然后调用weatherResponse保存方法请参阅我编写的链接。首先,保存sys对象,然后保存其余对象