Adm*_*kka 0 java int android list realm
我正在使用Realm Database创建Realm对象.我想将List声明integer
为我的对象.这是我的班级:
@PrimaryKey
private int id;
private String name;
private String image;
private String thumbnail;
private String message;
private int[] genre;
private int[] method;
Run Code Online (Sandbox Code Playgroud)
当我编译它时,我得到错误
Error:(12, 8) error: Type 'int[]' of field 'genre' is not supported
Run Code Online (Sandbox Code Playgroud)
怎么办?我试过List<Integer>
但没有运气.
然后我意识到Integer
对象没有延伸,RealmObject
所以我不能使用它.
任何想法都会有帮助.谢谢.
怎么办?我尝试使用List但没有运气.然后我意识到Integer对象没有扩展RealmObject所以我不能使用它.
好吧,你可以使用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;
Run Code Online (Sandbox Code Playgroud)
RealmInt.java
public class RealmInt extends RealmObject {
private int val;
public RealmInt() {
}
public RealmInt(int val) {
this.val = val;
}
// Getters and setters
}
Run Code Online (Sandbox Code Playgroud)
这就是你如何添加元素RealmList
:
RealmList<RealmInt> list = new RealmList<RealmInt>();
in.beginArray();
while (in.hasNext()) {
list.add(new RealmInt(in.nextInt()));
}
Run Code Online (Sandbox Code Playgroud)
然后你可以调用主RealmObject
类的setter 并传递list
.
[ 来源 ]
归档时间: |
|
查看次数: |
1114 次 |
最近记录: |