Android adapter to reorder by distance


I'm trying to reorder my list of items by distance (using getListView from Android, not a custom one) and I'm having problems.

I'm getting the spherical distance in meters (double type) using Maps Utils inside the adapter (SomeAdapter).

double distancia = SphericalUtil.computeDistanceBetween(fromCoord, toCoord);

But, after I fill the adapter (AsyncTask), I need to cut it by distance on onPostExecute and I have no idea how to do it.

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

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

Thank you.

asked by jdederle 05.10.2017 в 19:51

1 answer


implement Comparable interface in your Raids class -

class Raids implements Comparable<Raids> {
    private double distance;

public int compareTo(Raids instance2) {
    if (this.distance < instance2.distance)
        return -1;
    else if (this.distance > instance2.distance)
        return 1;
        return 0;

Then call Collections.sort -

ArrayList<Raids> some = pds.getAllRaids();

and update the adapter -

answered by 05.10.2017 / 19:58