将arraylist中的数据按升序打印到listview

时间:2017-06-12 10:53:28

标签: android android-volley

我是一个包含用户ID的arraylist。我想检查db中的id,并且必须通过volley从mysql中获取详细信息,并希望按照id的升序打印listview中的详细信息。如何才能实现此?

ArrayList<String> absenceids=new ArrayList<>();
Iterator<String> itr=absenceids.iterator();
 while (itr.hasNext()){
           getdetails(itr.next());
         }

我的getdetails()方法:

StringRequest stringRequest = new StringRequest(Request.Method.POST,URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray jsonResponse = new JSONObject(response).getJSONArray("Android");

                        int i=jsonResponse.length();

                        for(int j=0;j<i;j++) {
                            JSONObject jsonChildNode = jsonResponse.getJSONObject(j);
                          String user_id = jsonChildNode.optString("uid").toString();
                          String rollnum = jsonChildNode.optString("rollnum").toString();
                          String name = jsonChildNode.optString("name").toString();
                          /*  MA_id.add(rollnum);
                            Collections.sort(MA_id);
                            Toast.makeText(AttendenceSave.this, MA_id.toString(), Toast.LENGTH_SHORT).show();
                            MA_name.add(name);
                            Collections.sort(MA_name);*/
                           HashMap<String, String> contact = new HashMap<>();
                            contact.put("uid",user_id);
                            contact.put("rollnum",rollnum);
                            contact.put("name", name);

                            Listdetails.add(contact);
                            //CustomListAdapter2 adapter=new CustomListAdapter2(AttendenceSave.this,MA_id,MA_name);
                            ListAdapter adapter = new SimpleAdapter(AttendenceSave.this,Listdetails, R.layout.absenteeslayout, new String[]{"rollnum", "name"}, new int[]{R.id.abroll, R.id.abname});

                            absentees_list.setAdapter(adapter);
                            Utility.setListViewHeightBasedOnChildren(absentees_list);

1 个答案:

答案 0 :(得分:0)

unsortedList是输入,在以下操作之后,未排序的列表将包含以ID为升序的输出。

Collections.sort(unsortedList,new Comparator<String>() {
    @Override
    public int compare(String a, String b) {
        return a.compareTo(b);
    }
});