Android:如何为ListView排序数据?

时间:2013-03-01 03:28:58

标签: android

我从服务器上下载了一些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);

2 个答案:

答案 0 :(得分:4)

使用Collections.sort() with a custom comparator.

对列表进行排序
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));
    }
});

答案 1 :(得分:2)

与我的问题相同,但我现在已经开始工作了,感谢@rusmus。看看Sorting Double Data (distance) on 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
                    }                   
                });