Android 如何对此自定义RealmResult进行排序?
我需要按照最小距离的顺序排列我的领域结果。因此,我这样做:Android 如何对此自定义RealmResult进行排序?,android,realm,Android,Realm,我需要按照最小距离的顺序排列我的领域结果。因此,我这样做: public void onNext(final Location location) { latitude = location.getLatitude(); longitude = location.getLongitude(); nearbs = realm.where(Airport.class).greaterThanO
public void onNext(final Location location)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
nearbs = realm.where(Airport.class).greaterThanOrEqualTo("latitudeDeg",(latitude - 0.5)).lessThanOrEqualTo("latitudeDeg",(latitude + 0.5))
.greaterThanOrEqualTo("longitudeDeg",longitude - 0.5).lessThanOrEqualTo("longitudeDeg",longitude + 0.5).findAll();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm)
{
for(Airport airport : nearbs)
{
Location location1 = new Location("");
location1.setLatitude(airport.getLatitudeDeg());
location1.setLongitude(airport.getLongitudeDeg());
float distance = location1.distanceTo(location);
distance = distance/1852; //Calculate the distance
airport.setDistance(distance);
Log.d("distance",String.valueOf(distance));
realm.insertOrUpdate(airport);
}
nearbs.sort("distance", Sort.ASCENDING);
adapter.updateNearbs(nearbs); //here there is notifyDataSetChanged()
}
});
}
问题是Realm没有对这个列表进行排序,所以在adapter中,我没有按距离对机场进行排序
有更好的方法吗?
谢谢
在新的领域更新中,您应该将排序结果分配给列表nearbs=nearbs.sortdistance,Sort.升序;在新的领域更新中,您应该将排序结果分配给列表
nearbs = nearbs.sort("distance", Sort.ASCENDING);