如何使用Android HashMap排序<string,string =“”>距离值?

时间:2017-01-02 13:23:20

标签: android listview

我需要按 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) 中从最低到最高的距离对数据进行排序。我不知道该怎么做呢?这是我的代码,以获取数据不确定如何正确排序?

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

mininet> s1 ovs-ofctl add-flow "s1" in_port=1,actions:output=3

日志

mininet> s1 ovs-ofctl add-flow "s1" in_port=3,actions:output=1 

3 个答案:

答案 0 :(得分: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; }
}

答案 1 :(得分: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;
        }
    }
}

答案 2 :(得分: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();  
        }  
    }  
}