【问题标题】:I can not deal with the ActiveRecord android我无法处理 ActiveRecord android
【发布时间】: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


【解决方案1】:

根据此wiki https://github.com/pardom/ActiveAndroid/wiki/Saving-to-the-database,您需要将WeatherResponsesys 字段分配给另一个对象,如下所示:

Sys sys = new Sys();
sys.message = "something";
//another assignments
sys.save();

weatherResponse.sys = sys;
weatherResponse.save();

【讨论】:

  • 重点是我的对象是由 JSON 使用 Retrofit 形成的
  • 然后你需要解析你的JSON,从中创建Sys对象,并将该对象分配给weatherResponse.sys字段,然后调用WeatherResponsesave()方法
  • 查看我写的链接。这一切都必须进行改造
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
相关资源
最近更新 更多