2017-01-18 34 views
0

我需要这样做,以最小距离的顺序排列我的领域结果。所以我这样做:如何排序这个自定义RealmResult?

  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() 

       } 
      }); 

     } 

的问题是,境界不排序列表和使适配器我没有机场的距离排序。

有没有更好的方法来做到这一点? 感谢

+1

'nearbs = nearbs.sort( “距离”,Sort.ASCENDING);'在境界的新的更新,你应该分配排序结果到列表 –

回答

1
nearbs = nearbs.sort("distance", Sort.ASCENDING); 
在境界的新的更新

,你应该分配排序结果列出

+0

谢谢你的帮助! – ste9206