没有public RealmResults<E> 构造函数?

No public RealmResults<E> Constructor?

我有一个 table 有 Realm Objects 我正在呼叫 FooFoo 的其中一列指向另一列 Realm ObjectBar。 我想查询 table Foo 并挑选出所有我需要的 Bar 对象,然后将它们添加到 RealmBaseAdapter 中。

然而,据我所知,RealmBaseAdapter 在它的构造函数中只需要一个 RealmResults 列表。 我如何在不查询 Bar table 的情况下形成 BarRealmResults? 或者,我将如何查询 Foo table 并取回 BarRealmResults

例如,假设您有一个 table 的产品和产品细分,例如米花、玉米片、水果圈都属于谷物产品部分。我希望按某种规格查询 table 产品,并列出结果中包含的所有产品细分。

使用当前 RealmBaseAdapter 目前无法实现您所要求的内容,至少如果您想显示来自 Foo 查询的 Bar 对象,则无法实现。

如果您不想维护从 Bar 到 Foo 的关系,我建议您创建 RealmAdapter 而不是在能够过滤 Foo 查询以查找您想要显示的 Bar 对象的地方。 RealmBaseAdapter 包含的代码很少,因此应该很容易自定义:RealmBaseAdapter.java

由于无法直接执行此操作,我最终制作了自己的适配器。

 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 足够快,我可以在创建适配器时立即查询,而且我没有发现性能滞后 :)