2017-01-18 39 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