使用Realm和Parcel将API响应存储在两个不同的模型对象中

时间:2018-05-30 13:48:24

标签: android realm parcel

对于令人困惑的标题感到抱歉,但这是方案,

下面有一个API响应,

library(dplyr)

df %>% select(-ends_with(".1")) %>%
  mutate_at(vars(A:E), 
       funs(l = ifelse(lag(id)==id & lag(., default=0) == "1",acumulativediff,0)))


#    id       date A B C D E acumulativediff  A_l  B_l  C_l  D_l  E_l
# 1   1 2015-01-15 0 1 0 0 1               0    0    0    0    0    0
# 2   2 2004-03-01 1 0 1 0 1               0    0    0    0    0    0
# 3   2 2017-03-15 1 1 0 0 1            4762 4762    0 4762    0 4762
# 4   3 2000-01-15 0 0 0 1 0               0    0    0    0    0    0
# 5   4 2006-05-08 1 1 0 1 0               0    0    0    0    0    0
# 6   4 2008-05-09 1 0 1 1 0             732  732  732    0  732    0
# 7   4 2014-05-11 0 0 1 1 0            2925 2925    0 2925 2925    0
# 8   4 2014-06-11 0 0 1 0 0            2956    0    0 2956 2956    0
# 9   4 2014-07-11 1 1 1 1 1            2986    0    0 2986    0    0
# 10  4 2014-08-11 1 1 1 0 1            3017 3017 3017 3017 3017 3017
# 11  5 2015-12-19 1 1 0 1 1               0    0    0    0    0    0

我需要用这两个RealmObjets模拟这个响应,

"API_RESPONSE": {
  "id": "12345",
  "name": "Michael",
  "surname": "J.",
  "inventory"{
    ...
    ...
    }
}

所以这就是问题,如何使用这两个对象建模API响应?

1 个答案:

答案 0 :(得分:0)

public class ApiResponse {
    @SerializedName("API_RESPONSE")
    public ApiResponsePayload payload;
}

public class ApiResponsePayload {
    @SerializedName("id")
    public String id;
    @SerializedName("name")
    public String name;
    @SerializedName("surname")
    public String surname;
    @SerializedName("inventory")
    public Inventory inventory;

    public ApiResponsePayload() { // for gson
    }

    public ApiResponsePayload(ModelA modelA, ModelB modelB) {
        this.id = modelA.getId();
        this.name = modelA.getName();
        this.surname = modelA.getSurname();
        this.inventory = modelB.getInventory();
    }
}