我有一个像城市目录一样的Android应用程序,显示排序为Last Updated First的结果..我想将其更改为按用户位置的距离排序。 新的,需要帮助指向正确的方向...
答案 0 :(得分:0)
请在用户模型类
上使用波纹管分类机制Collections.sort(cityList,new Comparator(){
public int compare(City obj1, City obj2) {
// cityList --> ArrayList<City>
// obj1, obj2 are City objects and having location
// mLocation is user location
Location patent1 = new Location("hardcoded position");
patent1.setLatitude(obj1.getLatitude());
patent1.setLongitude(obj1.getLongitude());
Location patent2 = new Location("hardcoded position");
patent2.setLatitude(obj2.getLatitude());
patent2.setLongitude(obj2.getLongitude());
return mLocation.distanceTo(patent1) > mLocation.distanceTo(patent2) ? 1
: mLocation.distanceTo(patent1) < mLocation.distanceTo(patent2) ? -1
: 0;
}
});