Request nested Realm objects encapsulated in RealmList in RealmResults

I have the following RealmObject:

public class City extends RealmObject { private String cityId; private RealmList<Street> streets; public String getId() { return cityId; } public void setCityId(String cityId) { this.cityId = cityId; } public RealmList<Street> getStreets() { return streets; } public void setStreets(RealmList<Street> streets) { this.streets = streets; } } 

Now that I have a city, I need to query the streets of a specific city. How to do it? I tried:

  Realm.getInstance(context).where(City.class).equalTo("cityId", someCityId, false) .findFirst().getStreets().where().findAll() 

But this leads to an Exception. I need to display the streets in the ListView filtering implementation, so I need the streets to be RealmResults to use the RealmBaseAdapter<Street> .

+5
source share
1 answer

The correct way would be to open a Realm instance opened in your Activity in onCreate() and closed in onDestroy() , or in your custom application class .

Then you can use this instance of the region to query the region

 City city = realm.where(City.class).equalTo("cityId", cityId).findFirst(); 

Then you can access RealmList<T> , like any other list

 RealmList<Street> streets = city.getStreets(); 

You can then use recyclerview to get a view for the given index in your streets list.

0
source

All Articles