我正在尝试按距离重新排序我的项目列表(使用 android getListView,而不是自定义),但我遇到了问题。
我正在使用 Maps Utils inside 适配器 ( SomeAdapter ) 获取以米为单位的球面距离(双倍)。
double distance = SphericalUtil.computeDistanceBetween(fromCoord, toCoord);
但是在我填充适配器 (AsyncTask) 之后,我需要在我的 onPostExecute 上缩短距离,但我不知道。
@Override
protected void onPostExecute(Boolean result) {
try {
SQLiteHelper dbHelper = new SQLiteHelper(getActivity());
pds = new SomeDataSource(dbHelper.db);
ArrayList<Raids> some = pds.getAllRaids();
SomeAdapter listViewAdapter = new SomeAdapter(getActivity(), some);
getListView().setAdapter(listViewAdapter);
SharedPreferences somename = context.getSharedPreferences("SomeName", Context.MODE_PRIVATE);
Boolean UserOrder = somename.getBoolean("UserOrder", false);
if (UserOrder){
}
} catch (SQLiteException | NullPointerException s) {
Log.d("SomeName", "SomeFrag:", s);
}
}
谢谢
最佳答案
只需在您的Raids
类中实现Comparable
接口(interface)-
class Raids implements Comparable<Raids> {
private double distance;
...
@Override
public int compareTo(Raids instance2) {
if (this.distance < instance2.distance)
return -1;
else if (this.distance > instance2.distance)
return 1;
else
return 0;
}
}
然后对其调用Collections.sort
-
ArrayList<Raids> some = pds.getAllRaids();
Collections.sort(some);
并更新适配器-
listViewAdapter.notifyDataSetChanged();
关于java - Android 按距离重新排序适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073515/