2017-01-02 46 views
2

我需要按ListView中从最低到最高的距离排序数据。我不知道如何去做这件事?任何帮助赞赏。这是我的代码来抓取数据不知道如何让它正确排序?如何使用andriod进行排序HashMap <String,String>距离值?

*没有从实际数据获得的距离值,但是从代码本身获得的距离值。

public void showList(){ 
     try { 
      JSONObject jsonObj = new JSONObject(sJSON); //String sJSON 
      jsonArray = jsonObj.getJSONArray(TAG_RESULTS); //JSONArray jsonArray = null; 

      for (int i = 0; i < jsonArray.length(); i++) { 

       c = jsonArray.getJSONObject(i); //JSONObject c; 
       //String id = c.getString(TAG_ID); 
       name = c.getString(TAG_NAME); 
       address = c.getString(TAG_ADD); 


       String phone = c.getString(TAG_PHONE); 
       String dlatitude = c.getString(TAG_LAT); 
       String dlongitude = c.getString(TAG_LONG); 


       lat = Double.parseDouble(dlatitude); 
       longi = Double.parseDouble(dlongitude); 

       Location curlocation = new Location("curpoint"); 
       curlocation.setLatitude(latitude); 
       curlocation.setLongitude(longitude); 

       Location datalocation = new Location("datapoint"); 
       datalocation.setLatitude(lat); 
       datalocation.setLongitude(longi); 

       distance = curlocation.distanceTo(datalocation); 
       meter = Double.toString(distance); 

       HashMap<String, String> location = new HashMap<String,String(); 

       //location.put(TAG_ID,id); 
       location.put(TAG_NAME, name); 
       location.put(TAG_ADD, address); 
       location.put(TAG_PHONE, phone); 
       location.put(TAG_LAT, dlatitude); 
       location.put(TAG_LONG, dlongitude); 

       location.put(TAG_DIS, "DISTANCE" + meter + "m"); 

       slocationList.add(location); 

       ListAdapter adapter = new SimpleAdapter(CurrentLocationActivity.this, slocationList, R.layout.list_item,new String[]{TAG_NAME, TAG_ADD, TAG_PHONE, TAG_LAT,TAG_LONG,TAG_DIS},new int[]{R.id.name, R.id.address, R.id.phone,R.id.latitude,R.id.longitude, R.id.distance}); 

       list.setAdapter(adapter); 
     } 
    } 
} 

登录

at android.os.AsyncTask.finish(AsyncTask.java:679)              
at android.os.AsyncTask.access$500(AsyncTask.java:180)               
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:696)              
at android.os.Handler.dispatchMessage(Handler.java:102)              
at android.os.Looper.loop(Looper.java:150)              
at android.app.ActivityThread.main(ActivityThread.java:5639)              
at java.lang.reflect.Method.invoke(Native Method)               
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)              
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689) 
+0

什么是'slocationList'在上面的代码。 – Bethan

+0

用于指示洗车位置的应用程序。但是,当我试图对电源线进行编码时,屏幕消失了。也许,这是线程问题吗? –

+0

请发布您的AsyncTask代码。 – Bethan

回答

1

为此,您可以在许多方面,一个是:

Collections.sort(list, new Comparator() { 
    @Override 
    public int compare(Object o1, Object o2) { 
     Location l1 = (Location) o1; 
     Location l2 = (Location) o2; 

     return Double.compare(p1.getDistance(), p2.getDistance()); 
    } 
}); 

而且Location将是类:

public class Location { 
    private double mDistance; 
    private String mLongitude; 
    private String mLatitude; 
    private String name; 
    //add constructor for above parameters. 

    public double getDistance() { return mDistance; } 
} 
+0

谢谢,但我不明白它的工作效果。 –

+0

只需检查Java中的比较器。 – kenzo

0

这是给你修复代码,请点击此,

public void showList(){ 
    List<HashMap<String, String>> slocationList = new ArrayList<HashMap<String, String>>(); 
    try { 
     JSONObject jsonObj = new JSONObject(sJSON); //String sJSON 
     jsonArray = jsonObj.getJSONArray(TAG_RESULTS); //JSONArray jsonArray = null; 

     for (int i = 0; i < jsonArray.length(); i++) { 

      c = jsonArray.getJSONObject(i); //JSONObject c; 
      //String id = c.getString(TAG_ID); 
      name = c.getString(TAG_NAME); 
      address = c.getString(TAG_ADD); 


      String phone = c.getString(TAG_PHONE); 
      String dlatitude = c.getString(TAG_LAT); 
      String dlongitude = c.getString(TAG_LONG); 


      lat = Double.parseDouble(dlatitude); 
      longi = Double.parseDouble(dlongitude); 

      Location curlocation = new Location("curpoint"); 
      curlocation.setLatitude(latitude); 
      curlocation.setLongitude(longitude); 

      Location datalocation = new Location("datapoint"); 
      datalocation.setLatitude(lat); 
      datalocation.setLongitude(longi); 

      distance = curlocation.distanceTo(datalocation); 
      meter = Double.toString(distance); 

      HashMap<String, String> location = new HashMap<String,String>(); 

      //location.put(TAG_ID,id); 
      location.put(TAG_NAME, name); 
      location.put(TAG_ADD, address); 
      location.put(TAG_PHONE, phone); 
      location.put(TAG_LAT, dlatitude); 
      location.put(TAG_LONG, dlongitude); 

      location.put(TAG_DIS, meter); 

      slocationList.add(location); 
     } 
    } 
     Collections.sort(slocationList, new ListComparator());//This is the sorting part method 
     ListAdapter adapter = new SimpleAdapter(CurrentLocationActivity.this, slocationList, R.layout.list_item,new String[]{TAG_NAME, TAG_ADD, TAG_PHONE, TAG_LAT,TAG_LONG,TAG_DIS},new int[]{R.id.name, R.id.address, R.id.phone,R.id.latitude,R.id.longitude, R.id.distance}); 

     list.setAdapter(adapter);  

    //This is the sorting implementation method 
    public class ListComparator implements Comparator<HashMap<String, String>> { 
     @Override 
     public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) { 
      if (lhs.get(TAG_DIS) != null && rhs.get(TAG_DIS) != null) { 
       Double distance = Double.valueOf(lhs.get(TAG_DIS)); 
       Double distance1 = Double.valueOf(rhs.get(TAG_DIS)); 
       if (distance.compareTo(distance1) < 0) { 
        return -1; 
       } else if (distance.compareTo(distance1) > 0) { 
        return 1; 
      } else { 
       return 0; 
      } 
     } else { 
      return 0; 
     } 
    } 
} 
0

这是您的AsyncTask问题的解决方案..

public class MainActivity extends Activity { 

    String myJSON; 

    private static final String TAG_RESULTS="result"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_ADD ="address"; 

    JSONArray peoples = null; 

    ArrayList<HashMap<String, String>> personList; 

    ListView list; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     list = (ListView) findViewById(R.id.listView); 
     personList = new ArrayList<HashMap<String,String>>(); 
     GetDataJSON g = new GetDataJSON(); 
     g.execute("Your Api name....."); 
    } 

    protected void showList(){ 
     try { 
      JSONObject jsonObj = new JSONObject(myJSON); 
      peoples = jsonObj.getJSONArray(TAG_RESULTS); 

      for(int i=0;i<peoples.length();i++){ 
       JSONObject c = peoples.getJSONObject(i); 
       String id = c.getString(TAG_ID); 
       String name = c.getString(TAG_NAME); 
       String address = c.getString(TAG_ADD); 

       HashMap<String,String> persons = new HashMap<String,String>(); 

       persons.put(TAG_ID,id); 
       persons.put(TAG_NAME,name); 
       persons.put(TAG_ADD,address); 

       personList.add(persons); 
      } 

      ListAdapter adapter = new SimpleAdapter( 
       MainActivity.this, personList, R.layout.list_item, 
       new String[]{TAG_ID,TAG_NAME,TAG_ADD}, 
       new int[]{R.id.id, R.id.name, R.id.address} 
      ); 

      list.setAdapter(adapter); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 

    class GetDataJSON extends AsyncTask<String, Void, String>{ 

     @Override 
     protected String doInBackground(String... params) { 

      String uri = params[0]; 

      BufferedReader bufferedReader = null; 
      try { 
       URL url = new URL(uri); 
       HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
       StringBuilder sb = new StringBuilder(); 

       bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

       String json; 
       while((json = bufferedReader.readLine())!= null){ 
        sb.append(json+"\n"); 
       } 

       return sb.toString().trim(); 

      }catch(Exception e){ 
       return null; 
      } 



     } 

     @Override 
     protected void onPostExecute(String result){ 
      myJSON=result; 
      showList(); 
     } 
    } 
} 
+0

我把代码放在我的代码的顶端,并试图将代码放在我的代码上。但是,同样的问题发生。 http://blog.naver.com/teawong/220902526194如果你可以检查出来,请检查出来。 –