サーバーからデータをロードして ListView を作成しています。変数 (例: double) の値に応じてリストを並べ替えるにはどうすればよいですか? さらに役立つように、アプリはデバイス間の距離を計算し、リストに最小距離から最大距離までの星を付けたいと思います!ここに私のコードがあります.何かアイデアはありますか?
public class DriversList extends ListActivity{
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> driversList;
private static String url_all_drivers = "http://192.168.2.4/drivers/get_all_drivers.php";//192.168.2.4 , 10.0.2.2 , taxidroid.yzi.me
private static final String TAG_SUCCESS = "success";
private static final String TAG_DRIVERSID = "drivers";
private static final String TAG_ID = "id";
private static final String TAG_EMAIL="email";
private static final String TAG_NAME = "name";
private static final String TAG_SURNAME="surname";
private static final String TAG_TELEPHONE="telephone";
private static final String TAG_PLATE="plate";
private static final String TAG_CARMODEL="carmodel";
private static final String TAG_LATITUDE="latitude";
private static final String TAG_LONTITUDE="lontitude";
private static final String TAG_AVAILABLE="available";
private static final String DISTANCE="distance";
private static final String MILL="hh";
private double Clat,Clon;
JSONArray drivers = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drivers_list_layout);
driversList=new ArrayList<HashMap<String,String>>();
new LoadAllDrivers().execute();
Clat=getIntent().getExtras().getDouble("ClLat");
Clon=getIntent().getExtras().getDouble("ClLon");
Toast.makeText(getBaseContext(), ""+Clat+""+Clon, Toast.LENGTH_LONG).show();
}
class LoadAllDrivers extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DriversList.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();}
@Override
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_drivers, "GET", params);
Log.d("All Drivers: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
drivers = json.getJSONArray(TAG_DRIVERSID);
for (int i = 0; i < drivers.length(); i++) {
JSONObject c = drivers.getJSONObject(i);
String id = c.getString(TAG_ID);
String email=c.getString(TAG_EMAIL);
String name = c.getString(TAG_NAME);
String surname=c.getString(TAG_SURNAME);
String telephone=c.getString(TAG_TELEPHONE);
String plate=c.getString(TAG_PLATE);
String carmodel=c.getString(TAG_CARMODEL);
double lat=c.getDouble(TAG_LATITUDE);
double lon=c.getDouble(TAG_LONTITUDE);
String avail=c.getString(TAG_AVAILABLE);
double R = 6371; // earth’s radius (mean radius = 6,371km)
double dLat = Math.toRadians(Clat-lat);
double dLon = Math.toRadians(Clon-lon);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(Clat)) * Math.cos(Math.toRadians(lat)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c1 = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c1;
double gg=d*1000;
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_SURNAME, surname);
map.put(TAG_CARMODEL, carmodel);
if (d<1) {map.put(DISTANCE,String.format("%.2f", gg)); map.put(MILL, "m"); }
else {map.put(DISTANCE, String.format("%.2f", d)); map.put(MILL, "km"); }
driversList.add(map);
}
} else {runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(DriversList.this,"Αυτη τη στιγμη δεν υπαρχει διαθεσιμος οδηγος",Toast.LENGTH_SHORT).show();}});
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
DriversList.this, driversList,
R.layout.list_item, new String[] { TAG_ID,
TAG_NAME,TAG_SURNAME,TAG_CARMODEL,DISTANCE,MILL},
new int[] { R.id.id, R.id.name,R.id.surname,R.id.carmodel,R.id.txtdistance1,R.id.txtmorkm});
setListAdapter(adapter);
}
});
}
}
}
DISTANCE は、注文の基準にしたい変数です。事前に感謝します!