如何将ListView放入自定义顺序?

时间:2013-05-27 13:09:28

标签: android

我正在创建一个ListView,从我的服务器加载数据! 如何根据变量的值(例如double)来命令我的List? 为了帮助你更多,该应用程序计算设备之间的距离,我希望列表从最小到最大距离的星星!这是我的代码。任何想法?

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是我想要作为订单标准的变量。谢谢提前!

3 个答案:

答案 0 :(得分:1)

首先,为什么不创建一个类来代表您的驱动程序数据而不是使用HashMap?

无论如何,您可以使用Collections.sort完成所需的工作,您需要做的是:

  • 创建一个实现Comparator接口

    的类
    public class DriverComparator implements Comparator<HashMap<String, String>> {
    
    public int compare(HashMap<String, String> o1,
            HashMap<String, String> o2) {
        double distance1 = -1;
        double distance2 = -1;
        try {
        distance1 = Double.parseDouble(o1.get("DISTANCE"));
        } catch (NumberFormatException ex){
    
        } catch (NullPointerException nex){
    
        }
        try {
            distance2 = Double.parseDouble(o1.get("DISTANCE"));
            } catch (NumberFormatException ex){
    
            } catch (NullPointerException nex){
    
            }
        if (distance1 == distance2){
            return 0;
        }
        if (distance1 > distance2) {
            return 1;
        }
        return -1;
    }
    

    }

  • 在将列表添加到适配器之前对其进行排序:

     Collections.sort(list, new DriverComparator());
    

以下是有关比较器的优秀教程:Collections in Java

答案 1 :(得分:0)

您应该继承ListView适配器。创建一个排序功能。任何排序算法都应该做。然后将您的项目添加到适配器,对它们进行排序,最后将适配器设置为ListView。

答案 2 :(得分:0)

Listview与否它在这里并不重要。您应该做的只是将数据先前分类到列表视图。