如何在android中对List Fragment进行排序?

时间:2017-05-20 07:46:54

标签: android android-listfragment

public class ListFragmentDomesticPrice extends ListFragment {

    public static final String PREFS_NAME = "hotelsearch";
    public static final String PREF_NAME = "hotelfilter";
    private View V;
    private ProgressDialog pDialog = null;
    public ListFragmentDomesticPrice() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pDialog = new ProgressDialog(getActivity());
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading ...");
        V = new View(getActivity());
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {



        final List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
        List<RoomGuest> roomGuests = new ArrayList<RoomGuest>();
        final ImageLoaderAdapter adapter = new ImageLoaderAdapter(aList);
        setListAdapter(adapter);

        String url = "http://somesite.com/app_hotelresult";
        final SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);

        Map<String, Object> params = new HashMap();
        //  params.put("DepartureDate", "2017-05-02");
        params.put("Destination", "Delhi");//settings.getString("Destination", "").toString().trim());
        params.put("CheckInDate", "2017-06-12");//settings.getString("CheckInDate", "").toString());
        params.put("CheckOutDate", "2017-06-15");//settings.getString("CheckOutDate", "").toString());
        params.put("CountryCode", "IN");//settings.getString("CountryCode", "").toString());
        params.put("HotelName", "");//settings.getString("HotelName", "").toString());
        params.put("NoOfRooms", "1");//settings.getString("NoOfRooms", "").toString());

        RoomGuest roomGuest = new RoomGuest();
        List<Integer> childAge = new ArrayList<Integer>();
        childAge.add(Integer.parseInt(settings.getString("ChildAge1","").toString()));

        roomGuest.setNoOfChild(settings.getString("NoOfChild","").toString());
        roomGuest.setNoOfAdults(settings.getString("NoOfAdults","").toString());
        roomGuest.setChildAge(childAge);

        roomGuests.add(roomGuest);

        Gson gson = new Gson();
        Type type = new TypeToken<List<RoomGuest>>() {}.getType();
        String json = gson.toJson(roomGuests, type);

        Log.d("json",json);

        try {
            params.put("RoomGuests", new JSONArray(json));
        }
        catch (JSONException e){

        }
        JSONObject parameters = null;
        try {
            parameters = new JSONObject(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        pDialog.show();

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("Response ", response.toString());
                pDialog.dismiss();
                // Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
                try {
                    response = new JSONObject(response.toString());
                    JSONObject hotel = new JSONObject(response.getString("HotelSearchResult"));
                    JSONArray datas = hotel.getJSONArray("HotelResults");

                    if(datas.length() > 0) {

                        for (int i = 0; i < datas.length(); i++) {

                            JSONObject data = null;
                            data = datas.getJSONObject(i);
                            Log.d("Create Response", data.getString("HotelName"));

                            HashMap<String, String> hm = new HashMap<String, String>();
                            hm.put("traceId",hotel.getString("TraceId"));
                            hm.put("resultIndex",data.getString("ResultIndex"));
                            hm.put("HotelName", data.getString("HotelName"));
                            hm.put("HotelPicture", data.getString("HotelPicture"));
                            if(!data.getString("HotelLocation").equals(null) && !data.getString("HotelLocation").equals("null"))
                                hm.put("HotelLocation", data.getString("HotelLocation"));
                            else
                                hm.put("HotelLocation", "");
                            hm.put("HotelStar", data.getString("StarRating"));

                            JSONObject price = data.getJSONObject("Price");
                            hm.put("HotelPrice",settings.getString("Currency", "").toString()+" " +price.getString("OfferedPriceRoundedOff"));
                            aList.add(hm);
                            adapter.notifyDataSetChanged();
                        }


                    }
                    else{

                        Toast.makeText(getActivity(), "No results found!", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(getActivity(), "No results found!", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Error : ", error.toString());
                pDialog.dismiss();
                //Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
            }
        });

        MyApplication.getInstance().addToRequestQueue(request);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

}

我想根据价格和名称对列表视图进行排序。酒店价格和酒店名称有一个单独的栏目。我使用collections.reverse

颠倒了列表
 Collections.reverse(aList);
 adapter.notifyDataSetChanged();

是否可以使用比较器进行排序?

1 个答案:

答案 0 :(得分:0)

我们可以像往常一样按照ListViews的方式对ListFragment进行排序。

                    Collections.sort(aList, new Comparator<Map<String, String>>() {

                        final static String COMPARE_KEY = "HotelName";

                        @Override
                        public int compare(Map<String, String> lhs, Map<String, String> rhs) {
                            String v1 = lhs.get(COMPARE_KEY);
                            String v2 = rhs.get(COMPARE_KEY);
                            return v1.compareTo(v2);
                        }
                    });
                    adapter.notifyDataSetChanged();