Android adapter to reorder by distance

3

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.

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

Thank you.

    
asked by jdederle 05.10.2017 в 19:51
source

1 answer

3

implement Comparable interface in your Raids class -

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

Then call Collections.sort -

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

and update the adapter -

listViewAdapter.notifyDataSetChanged();
    
answered by 05.10.2017 / 19:58
source