Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

android - How to sort listview items in descending order

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());
}

}

But the result I'm getting is:

5
15
14
0
0

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the following code to sort your integer list is descending order.Here we are overriding compare() so that it sorts in descending order.

//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;
                     } 
                   }
            });

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...