如何降序排列列表视图项目进行排序视图、排列、项目、降序

2023-09-05 04:05:07 作者:这个年纪太尴尬

所以我有一个列表视图,我想降序排列NumberOfRecords排序。我有一个自定义的数组适配器,但我打电话给我的分选上课前,我把一个数据,我ArrayList中,这是我的AsyncTask接收JSON:

So I have a listview where I wanted to sort the NumberOfRecords in descending order. I have a custom array adapter but I called my sorting class before I place a data in my ArrayList, this my Asynctask receiving 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);
    }
}

这是我的排序类:

And here's my sorting class:

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

是我整理的实施错了吗?或者我应该分析它以整数返回前?

Is my sorting implementation wrong? Or should I parse it to Integer before return?

推荐答案

好了,所以我通过更​​换解决了这个的:

Okay, so I solved this by replacing the:

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

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

所以简单的在一个字符串数据类型的整数比较不正确的结果,但要首先分析字符串:

So the simple compare of an integer in a String data type would not result correctly but to parse the string first by:

的Integer.parseInt(字符串)

和获取数字字符串的真正价值。

and get the true value of the number string.