1

我有一些从服务器中提取的 JSON 数据。此数据中的字段之一是距离值。我需要在 ListView 中按从最低到最高的距离对数据进行排序。我不知道该怎么做?

任何帮助表示赞赏。

这是我获取数据的代码,不知道如何正确排序?

// Hashmap for ListView
ArrayList<HashMap<String, String>> bathroomList = new ArrayList<HashMap<String, String>>();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.activity_search);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        bathrooms = json.getJSONArray(TAG_BATHROOMS);

        // looping through All Contacts
        for(int i = 0; i < bathrooms.length(); i++){
            JSONObject c = bathrooms.getJSONObject(i);


            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String access = c.getString(TAG_ACCESS);
            String city = c.getString(TAG_CITY);
            String comment = c.getString(TAG_COMMENT);
            String directions = c.getString(TAG_DIRECTIONS);
            String name = c.getString(TAG_NAME);
            String street = c.getString(TAG_STREET);
            String bathroomtype = c.getString(TAG_BATHROOMTYPE);                
            String distance = c.getString(TAG_DISTANCE);
            String distanceTrimmed = distance.substring(0,4) + " " + "miles away";
            String avail = c.getString(TAG_AVAIL);
            String country = c.getString(TAG_COUNTRY);
            String state = c.getString(TAG_STATE);
            String postal = c.getString(TAG_POSTAL);

            //System.out.println(name);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_ACCESS, access);
            map.put(TAG_CITY, city);
            map.put(TAG_COMMENT, comment);
            map.put(TAG_DIRECTIONS, directions);
            map.put(TAG_NAME, name);
            map.put(TAG_STREET, street);
            map.put(TAG_BATHROOMTYPE, bathroomtype);
            map.put(TAG_DISTANCE, distanceTrimmed);
            map.put(TAG_AVAIL, avail);
            map.put(TAG_COUNTRY, country);
            map.put(TAG_STATE, state);
            map.put(TAG_POSTAL, postal);

            // adding HashList to ArrayList
            bathroomList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    /**
     * Updating parsed JSON data into ListView
     * */

    ListAdapter adapter = new SimpleAdapter(this, bathroomList,
            R.layout.list_item,
            new String[] { TAG_ID, TAG_NAME, TAG_STREET, TAG_CITY, TAG_STATE, TAG_POSTAL,TAG_COUNTRY, TAG_DISTANCE, TAG_DIRECTIONS, TAG_COMMENT, TAG_AVAIL, TAG_BATHROOMTYPE, TAG_ACCESS}, new int[] {
                     R.id.key,R.id.name, R.id.street, R.id.city, R.id.state, R.id.postal,R.id.country, R.id.distance, R.id.directions, R.id.comments, R.id.availability, R.id.bathroomtype, R.id.access });

    setListAdapter(adapter);
4

2 回答 2

4

使用自定义比较器对列表进行排序。Collections.sort()

Collections.sort(bathroomList, new Comparator<HashMap<String, String>>()
{
    @Override
    public int compare(HashMap<String, String> a, HashMap<String, String> b)
    {
        return a.get(TAG_DISTANCE).compareTo(b.get(TAG_DISTANCE));
    }
});
于 2013-03-01T03:38:12.493 回答
2

我的也有同样的问题,但我现在已经解决了,感谢@rusmus。看看ListView 上的双精度数据(距离)排序

试试这个代码:

Collections.sort(bathroomList, new Comparator<HashMap<String, String>>() {

                    @Override
                    public int compare(HashMap<String, String> o1,
                            HashMap<String, String> o2) {                           
                        return Double.compare(Double.parseDouble(o1.get(TAG_DISTANCE)), Double.parseDouble(o2.get(TAG_DISTANCE))); // error
                    }                   
                });
于 2014-02-10T16:53:05.367 回答