No public RealmResults <E> Constructor?

I have a table with Realm ObjectsI'm calling Foo. One column Foorefers to another Realm Object, Bar. I want to query a table Fooand select all the objects Barthat I need, and then add them to RealmBaseAdapter.

However, as far as I know, it RealmBaseAdapteraccepts only a RealmResultslist in it . How do I form RealmResultsof Barunsolicited table Bar? Or, how would I query the table Fooand return RealmResultsfrom Bar?

For example, let's say that you have a table of products and product segments, for example. rice crises, corn flakes, fruit loops, they all belong to the segment of cereal products. I want to query the product table for a specific specification and list all the product segments contained in the result.

+4
source share
2 answers

Since there was no way to do this directly, I ended up creating 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 to oversee the request from Foo → Bar, which would give me the results I wanted. In the end, it looked 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);

. , Realm , , , :)

+4

, , RealmBaseAdapter, , Bar Foo.

Bar Foo, RealmAdapter, Foo Bar, . RealmBaseAdapter , : RealmBaseAdapter.java

0

Source: https://habr.com/ru/post/1584530/


All Articles