我有一个包含 Realm 对象
的表,我正在调用Foo
。
Foo
的其中一列指向另一个 Realm Object
,Bar
。
我想查询表 Foo
并挑选出我需要的所有 Bar
对象,然后将它们添加到 RealmBaseAdapter
。
但是,据我所知,RealmBaseAdapter
仅在其构造函数中使用了一个 RealmResults
列表。
我如何在不查询 Bar
表的情况下形成 Bar
的 RealmResults
?
或者,我如何查询 Foo
表并返回 Bar
的 RealmResults
?
例如,假设您有一张包含产品和产品分割的表格,例如米花、 Jade 米片、水果圈都属于 Cereal 产品部分。我希望按某种规范查询产品表,并列出结果中包含的所有产品段。
最佳答案
由于无法直接执行此操作,我最终制作了自己的适配器。
public class BarAdapter extends ArrayAdapter<Bar> {
//code to instantiate the adapter, inflate views, etc
}
这部分是微不足道的,唯一需要完成的艰苦工作是从 Foo-->Bar 策划一个查询,这将使我获得我想要的结果。它最终看起来像这样,
// where fooType was what I wanted to ween out the Foo results on before
// selecting Bar objects.
RealmQuery<Foo> fooRealmQuery = realm
.where(Foo.class)
.equalTo("fooType", "desired type")
.or()
.equalTo("fooType", "other type");
RealmResults<Foo> fooList = fooRealmQuery.findAll();
List<Bar> barList = new ArrayList<Bar>();
for (Foo foo : fooList) {
Bar bar = foo.getBar();
if (!barList.contains(bar)) {
barList.add(bar);
Log.d(TAG, "added " + bar.getName());
} else {
Log.d(TAG, "I already had that bar");
}
}
adapter = new BarAdapter(this, barList);
listView.setAdapter(adapter);
现在一切正常。此外,Realm 的速度足够快,我可以在创建适配器时立即查询,而且我看不到性能滞后 :)
关于java - 没有 public RealmResults<E> 构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29852308/