1

我的 JSON 数据从服务器 api 看起来像这样

{
  //...
  PredecessorIds:[[1,2][3,4][5]]
  //...
}

我可以成功处理 Integer 或 String 的数组,RealmList<RealmInt>但是这次我失败了,因为 RealmList> 不支持说,"Type parameter 'io.realm.realmList' is not within its bounds...."

RealmInt参阅此链接

我尝试使用RealmList<RealmLista>RealmLista 扩展的地方来解决它,RealmObject并且有RealmList这样的

public class RealmLista extends RealmObject {
public RealmList<RealmInt> value;
public RealmLista() {
}

public RealmLista(RealmList<RealmInt> val) {
  this.value = val;
}

}

然后创建 aRealmListaTypeAdapter并将其添加到 Gson 但在反序列化时Gson expects an Object (RealmLista) but is found array,如上面显示的来自服务器的数据是显而易见的。

//RealmListAdapter for Gson
@Override
public RealmLista read(JsonReader in) throws IOException {
    RealmLista lista = new RealmLista();
    Gson gson = new Gson();
    //how to read that [[1],[3,4]] int into RealmLista
    in.beginArray();
    while (in.hasNext()) {
        lista.value.add(new RealmInt(in.nextInt()));
    }
    in.endArray();
    return lista;
}

有什么方法可以在保存的同时List<List<Integer>>转换为RealmObject任何类型来存储简单List<List<Integer>>,Gson很容易转换。:-/

4

1 回答 1

1

Realm 目前不支持列表列表。请参阅 https://github.com/realm/realm-java/issues/2549

因此,@EpicPandaForce 关于创建一个包含该内部列表的 RealmObject 的想法可能是最好的解决方法。

它可能看起来像这样:

public class Top extends RealmObject {
  private RealmList<ChildList> list;
}

public class ChildList extends RealmObject {
  private RealmList<RealmInt> list;
}

public class RealmInt extends RealmObject {
  private int i;
}

要点的正确链接应该是:https ://gist.github.com/cmelchior/1a97377df0c49cd4fca9

于 2016-05-04T10:04:51.497 回答