gpt4 book ai didi

java - 错误: Type 'int[]' of field is not supported | how to add integer list to Realm?

转载 作者:行者123 更新时间:2023-12-01 06:49:38 24 4
gpt4 key购买 nike

我正在使用 Realm 数据库创建 Realm 对象。我要申报名单integer到我的对象。这是我的课:

@PrimaryKey
private int id;
private String name;
private String image;
private String thumbnail;
private String message;
private int[] genre;
private int[] method;

当我编译它时,出现错误

Error:(12, 8) error: Type 'int[]' of field 'genre' is not supported

这该怎么办?我尝试过 List<Integer>但运气不佳。

然后我意识到Integer对象不扩展 RealmObject所以我不能使用它。

任何想法都会有帮助。谢谢。

最佳答案

What to do with this? I tried with List but no luck. Then i realized that Integer object doesn't extends RealmObject so I cannot use it.

那么,您可以使用 RealmList为此目的:

@PrimaryKey
private int id;
private String name;
private String image;
private String thumbnail;
private String message;
private RealmList<RealmInt> genre;
private RealmList<RealmInt> method;

RealmInt.java

public class RealmInt extends RealmObject {
private int val;

public RealmInt() {
}

public RealmInt(int val) {
this.val = val;
}

// Getters and setters
}

这就是向 RealmList 添加元素的方法:

RealmList<RealmInt> list = new RealmList<RealmInt>();
in.beginArray();
while (in.hasNext()) {
list.add(new RealmInt(in.nextInt()));
}

然后您可以调用主RealmObject类的setter并传递list

[SOURCE ]

关于java - 错误: Type 'int[]' of field is not supported | how to add integer list to Realm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42085138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com