如何按降序对列表视图项进行排序

时间:2013-08-12 12:53:45

标签: android sorting android-listview arraylist android-arrayadapter

所以我有一个listview,我想按降序排序NumberOfRecords。我有一个自定义数组适配器但我在将数据放入ArrayList之前调用了我的排序类,这是我的Asynctask接收JSON:

public class SampleListTask extends AsyncTask<String, Void, String> {

    public ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(SampleActivity.this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... path) {
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

        Log.d(Constant.TAG_RANKING, path[0]);
        String apiRequestReturn = UtilWebService.getRequest(path[0]);
        if (apiRequestReturn.equals("")) {
            Log.d(Constant.TAG_SAMPLE, "WebService request is null");
            return null;
        } else {
            Log.d(Constant.TAG_SAMPLE, "WebService request has data");
            return apiRequestReturn;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }

        if (null == result || result.length() == 0) {
            application.shortToast("No data found from server");
        } else {
            try {
                JSONObject sampleObject = new JSONObject(result);
                JSONArray jsonArray = sampleObject
                        .getJSONArray(Constant.TAG_SAMPLE);

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

                    sample = new ArraySample();

                    sample.setId(objJson.getInt(Constant.TAG_SONGID));
                    sample.setThumbUrl(objJson
                            .getString(Constant.TAG_IMAGEURL));
                    sample.setTitle(objJson
                            .getString(Constant.TAG_NAME));
                    sample.setArtist(objJson
                            .getString(Constant.TAG_ARTIST));
                    sample.setDuration(Utility
                            .changeStringTimeFormat(objJson
                                    .getString(Constant.TAG_MUSICLENGTH)));
                    sample.setNumberOfRecords(objJson
                            .getString(Constant.TAG_NUMBEROFRECORDS));

                    Collections.sort(sampleList, new SortByRecordNumber()); // This where I call the class
                    sampleList.add(sample);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            setAdapterToListview();
        }
    }

    public void setAdapterToListview() {
        objRowAdapter = new RowAdapterSample(getApplicationContext(),
                R.layout.item_sample, sampleList);
        sampleListView.setAdapter(objRowAdapter);
    }
}

这是我的排序类:

public class SortByRecordNumber implements Comparator {

public int compare(Object o1, Object o2) {
    ArraySample p1 = (ArraySample) o1;
    ArraySample p2 = (ArraySample) o2;

    return p2.getNumberOfRecords().compareTo(p1.getNumberOfRecords());
}

}

但我得到的结果是:

5
15
14
0
0

我的排序实现错了吗?或者我应该在返回之前将其解析为Integer?

3 个答案:

答案 0 :(得分:6)

您可以使用以下代码对整数列表进行降序排序。这里我们重写compare(),以便按降序排序。

//sort list in desc order 
            Collections.sort(myIntegerList, new Comparator<Integer>() {
                public int compare(Integer one, Integer other) {
                     if (one >= other) {
                         return -1;
                     } else {
                         return 1;
                     } 
                   }
            });

希望它有所帮助。

答案 1 :(得分:2)

好的,所以我通过替换:

解决了这个问题

p2.getNumberOfRecords().compareTo(p1.getNumberOfRecords())

为:

(int) Integer.parseInt(p2.getNumberOfRecords()) - Integer.parseInt(p1.getNumberOfRecords())

因此,String数据类型中的整数的简单比较不会正确结果,而是先解析字符串:

Integer.parseInt(string)

并获取数字字符串的真值。

答案 2 :(得分:2)

尝试使用此比较器。

Comparator objComparator = new Comparator() {
    public int compare(Object o1, Object o2) {
        int no1 = Integer.parseInt((String) o1);
        int no2 = Integer.parseInt((String) o2);
        return  no1 < no2 ? -1 : no1 == no2 ? 0 : 1;
    }
};
Collections.sort(myIntegerList, objComparator);
相关问题