2017-01-02 2 views
2

Ich muss die Daten nach Entfernung von niedrigsten zum höchsten im ListView sortiert haben. Ich bin mir nicht sicher, wie ich das machen soll? Jede Hilfe wird geschätzt. Dies ist mein Code, um die Daten nicht sicher zu bekommen, wie man sie richtig sortiert?Wie sortiere ich mit andriod HashMap <String, String> Abstandswert?

* Die Entfernungswerte wurden nicht aus den tatsächlichen Daten erhalten, aber die Entfernungswerte wurden aus dem Code selbst erhalten.

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

Log

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

Was 'slocationList' im obigen Code ist. – Bethan

+0

Wird für Apps verwendet, die den Standort der Autowaschanlage angeben. Als ich jedoch versuchte, das Kabel zu codieren, verschwand der Bildschirm. Vielleicht ist es ein Problem mit Thread? –

+0

Bitte senden Sie Ihren AsyncTask-Code. – Bethan

Antwort

1

Sie dies auf vielfältige Weise tun können, ist:

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

Und Location wird Klasse:

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

Danke, aber ich verstehe nicht, wie gut es funktioniert. –

+0

Vergleichen Sie nur Komparatoren in Java. – kenzo

0

Hier ist der Code für Ihre Verlegenheit, folgen Sie bitte diesem

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

Dies ist die Lösung für Ihre AsyncTask Ausgabe ..

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

Ich legte den Code auf die Spitze meines Codes und versuchte, den Code auf meinen Code zu setzen.Jedoch tritt das gleiche Problem auf. http://blog.naver.com/teawong/220902526194 Wenn Sie es überprüfen können, überprüfen Sie es bitte. –

Verwandte Themen