简体   繁体   English

没有公开的RealmResults <E> 构造函数?

[英]No public RealmResults<E> Constructor?

I've got a table that has Realm Objects I'm calling Foo . 我有一个有Realm Objects的桌子我叫Foo One of the columns of Foo points to another Realm Object , Bar . 其中一个Foo指向另一个Realm ObjectBar I want to query table Foo and pick out all of the Bar objects I need, and then add them to a RealmBaseAdapter . 我想查询表Foo并选择我需要的所有Bar对象,然后将它们添加到RealmBaseAdapter

However, to my knowledge, RealmBaseAdapter only takes a RealmResults list in it's constructor. 但是,据我所知, RealmBaseAdapter只在其构造函数中包含一个RealmResults列表。 How would I form a RealmResults of Bar without querying the Bar table? 如何在不查询Bar表的情况下形成BarRealmResults Or, how would I query the Foo table and get back a RealmResults of Bar ? 或者,我如何查询Foo表并获取BarRealmResults

For example, say you had a table of product's and product segments, eg rice krispies, corn flakes, fruit loops would all belong to the cereal product segment. 例如,假设您有一张产品和产品细分表,例如大米krispies,玉米片,水果圈都属于谷物产品细分市场。 I wish to query a table of products by some specification, and list all of the product segments that are contained in the result. 我希望按一些规范查询产品表,并列出结果中包含的所有产品分段。

Since there was no way to do this directly, I ended up making my own adapter. 由于没有办法直接这样做,我最终制作了自己的适配器。

 public class BarAdapter extends ArrayAdapter<Bar>  {

      //code to instantiate the adapter, inflate views, etc

 }

This part was trivial, the only hard work to be done was by curating a query from Foo-->Bar that would get me the results that I wanted. 这部分是微不足道的,唯一要做的艰苦工作是通过策划来自Foo - > Bar的查询来获取我想要的结果。 It ended up looking something like this, 它最终看起来像这样,

    // 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);

Now things work well. 现在情况很好。 Also, Realm is fast enough that I can query right as I'm creating the adapter and I see no performance lag :) 此外,Realm足够快,我可以正确查询,因为我正在创建适配器,我看到没有性能滞后:)

What you are asking is currently not possible using the current RealmBaseAdapter , at least not if you want to display Bar objects from a Foo query. 您目前无法使用当前的RealmBaseAdapter ,至少在您想要从Foo查询中显示Bar对象时是这样。

If you don't want to maintain a relationship from Bar to Foo, I would recommend creating your RealmAdapter instead where able to filter your Foo query for the Bar objects you wanted to display. 如果你不想保持从Bar到Foo的关系,我建议你创建你的RealmAdapter而不是过滤你想要显示的Bar对象的Foo查询。 The RealmBaseAdapter contains very little code, so it should be fairly easy to customize: RealmBaseAdapter.java RealmBaseAdapter包含非常少的代码,因此它应该很容易定制: RealmBaseAdapter.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM