I am getting my JSON data from the web. With many objects, one of these are latitude and longitude. I can calulate the exact distance but..
How can I sort the listview to show the nearest location first.
MainActivity
public class MainActivity extends ActionBarActivity {
private ListView lv_nearestShops;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GPSTracker gps;
this.lv_nearestShops = (ListView) this.findViewById(R.id.lv_nearestShops);
gps = new GPSTracker(MainActivity.this);
if(gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongtitude();
Log.d("Location = ", "Latitude: " + latitude + " - Longitude : " + longitude);
}
new GetAllShopsTask().execute(new ApiConnector());
}
public void setListAdapter(JSONArray jsonArray){
this.lv_nearestShops.setAdapter(new lv_nearestShops_adapter(jsonArray, this));
}
private class GetAllShopsTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].GetAllShops();
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
setListAdapter(jsonArray);
}
}
}
ApiConnector
public class ApiConnector {
public JSONArray GetAllShops() {
String url = ""; //php script
HttpEntity httpEntity = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
}
listview Adapter
public class lv_nearestShops_adapter extends BaseAdapter {
private JSONArray dataArray;
private Activity activity;
double latA;
double lngA;
double latB;
double lngB;
private static LayoutInflater inflater = null;
public lv_nearestShops_adapter(JSONArray jsonArray, Activity a)
{
this.dataArray = jsonArray;
this.activity = a;
inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.dataArray.length();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.lv_nearestshop_layout, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.itemName);
viewHolder.street = (TextView) convertView.findViewById(R.id.itemStreet);
viewHolder.distance = (TextView) convertView.findViewById(R.id.itemDistance);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
try {
JSONObject jsonObject = this.dataArray.getJSONObject(position);
// naam
viewHolder.name.setText(jsonObject.getString("name"));
// street
viewHolder.street.setText(jsonObject.getString("street"));
// distance
latB = jsonObject.getDouble("latitude");
lngB = jsonObject.getDouble("longitude");
float distance;
GPSTracker gps;
Context context = parent.getContext();
gps = new GPSTracker(context);
if(gps.canGetLocation()) {
latA = gps.getLatitude();
lngA = gps.getLongtitude();
Log.e("Location = ", "Latitude: " + latA + " - Longitude : " + lngA);
}
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
int iDistance= (int) distance;
if(iDistance > 1000) {
int result = iDistance / 1000;
viewHolder.distance.setText(String.valueOf(result) + " km");
} else {
viewHolder.distance.setText(String.valueOf(iDistance) + " m");
}
jsonObject.put("distance", iDistance);
}
catch (JSONException e) {
e.printStackTrace();
}
return convertView;
}
private class ViewHolder{
private TextView name;
private TextView street;
private TextView distance;
}
}