我正在尝试将RealmList的RealmList从服务器(JSON)获取到realm对象。我正在
错误:RealmList的元素类型必须是实现“ RealmModel”的类,或者是“ java.lang.String”,“ byte []”,“ java.lang.Boolean”,“ java.lang.Long”之一,'java.lang.Integer','java.lang.Short','java.lang.Byte','java.lang.Double','java.lang.Float','java.util.Date'。< / p>
{
"facilities": [
{
"facility_id": "1",
"name": "Property Type",
"options": [
{
"name": "Apartment",
"icon": "apartment",
"id": "1"
},
{
"name": "Condo",
"icon": "condo",
"id": "2"
},
{
"name": "Boat House",
"icon": "boat",
"id": "3"
},
{
"name": "Land",
"icon": "land",
"id": "4"
}
]
},
{
"facility_id": "2",
"name": "Number of Rooms",
"options": [
{
"name": "1 to 3 Rooms",
"icon": "rooms",
"id": "6"
},
{
"name": "No Rooms",
"icon": "no-room",
"id": "7"
}
]
},
{
"facility_id": "3",
"name": "Other facilities",
"options": [
{
"name": "Swimming Pool",
"icon": "swimming",
"id": "10"
},
{
"name": "Garden Area",
"icon": "garden",
"id": "11"
},
{
"name": "Garage",
"icon": "garage",
"id": "12"
}
]
}
],
"exclusions": [
[
{
"facility_id": "1",
"options_id": "4"
},
{
"facility_id": "2",
"options_id": "6"
}
],
[
{
"facility_id": "1",
"options_id": "3"
},
{
"facility_id": "3",
"options_id": "12"
}
],
[
{
"facility_id": "2",
"options_id": "7"
},
{
"facility_id": "3",
"options_id": "12"
}
]
]
}
答案 0 :(得分:0)
尝试此解决方案
public class Exclusion extends RealmObject {
private int facilityId;
private int optionsId;
}
public class Exclusions extends RealmObject {
private RealmList<Exclusion> exclusions;
}
所以现在您可以使用RealmList<Exclusions> exclusionsRealmList
作为合法的领域列表
希望这会有所帮助
答案 1 :(得分:0)
Gianthran的答案很接近,但您也应该建立关系。
public class Exclusion extends RealmObject {
@Index
private int facilityId;
@Index
private int optionsId;
private Facility facility;
private Option option;
}
和
public class Facility extends RealmObject {
@LinkingObjects("facility")
private final RealmResults<Exclusion> exclusions = null;
}
public class Option extends RealmObject {
@LinkingObjects("option")
private final RealmResults<Exclusion> exclusions = null;
}
我认为Exclusions
对象不是必需的,我希望该表在某种程度上已经是一个集合。
答案 2 :(得分:0)
在使用retorfit 2时如何使用嵌套的RealmList?并摆脱错误:RealmList的元素类型必须是实现“ RealmModel”的类
假设我的Json是这样的:
{
"data": {
"options": [
[
{
"name": "abc",
"id": 1,
"type": "profile"
},
{
"name": "def",
"id": 2,
"type": "profile"
}
],
[
{
"name": "abc",
"id": 1,
"type": "business_profile"
},
{
"name": "def",
"id": 2,
"type": "business_profile"
}
]
]
}
}
我的POJO类似于
-----------------------------------com.example.Data.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Data {
@SerializedName("options")
@Expose
private List<List<Option>> options = null;
public List<List<Option>> getOptions() {
return options;
}
public void setOptions(List<List<Option>> options) {
this.options = options;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("data")
@Expose
private Data data;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
-----------------------------------com.example.Option.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Option {
@SerializedName("name")
@Expose
private String name;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("type")
@Expose
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
将对象转换为RealmObject时,我需要将RealmObject扩展到我的对象类,但是我需要将List替换为RealList,这在一个级别上是可能的,但是在编写RealmList>时,会给我错误。
我试图通过创建GenericList来解决。
public class GenericList<T extends RealmObject> extends RealmObject {
private RealmList<T> fieldList = new RealmList<>();
public RealmList<T> getFieldList() {
return fieldList;
}
public void setFieldList(RealmList<T> fieldList) {
this.fieldList = fieldList;
}
}
在Pojo中进行了更改,例如
public class Data {
@SerializedName("options")
@Expose
private RealmList<GenericList<Option>> options = null;
public RealmList<GenericList<Option>> getOptions() {
return options;
}
public void setOptions(RealmList<GenericList<Option>> options) {
this.options = options;
}
}
但这会产生错误:预期