0

I am sorting JSONArray and show them in a custom list view, but after sorting the data does not changed in custom list view.

Here is my code for fab button to select which sort is to be perform:

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        AlertDialog.Builder builder=new AlertDialog.Builder(SearchResult.this);
        builder.setTitle("Sort List Item");
        builder.setItems(new CharSequence[]{"By Name", "By Father Name","By EPIC","By House Number"}, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                switch (i)
                {
                    case 0:
                        Toast.makeText(getApplicationContext(), "By Name", Toast.LENGTH_SHORT).show();

                        sortJsonTechnique="1";
                        if (myData)
                        {
                            sortByName(sortdata,sortJsonTechnique);
                        }
                        break;
                    case 1:

                        Toast.makeText(getApplicationContext(), "By Father Name", Toast.LENGTH_SHORT).show();
                        sortJsonTechnique="2";
                        if (myData)
                        {
                            sortByName(sortdata,sortJsonTechnique);
                        }

                        break;
                    case 2:

                        Toast.makeText(getApplicationContext(), "By EPIC", Toast.LENGTH_SHORT).show();
                        sortJsonTechnique="3";
                        if (myData)
                        {
                            sortByName(sortdata,sortJsonTechnique);
                        }

                        break;
                    case 3:

                        Toast.makeText(getApplicationContext(), "By House Number", Toast.LENGTH_SHORT).show();
                        sortJsonTechnique="4";
                        if (myData)
                        {
                            sortByName(sortdata,sortJsonTechnique);
                        }

                        break;
                    default:
                        break;
                }

            }
        });
        builder.show();


    }
});

And after selection made in fab button click i sort json array. Here is code for parse json array:

 private void sortByName(String mysortJson,String sortType)
    {

        List<JSONObject> jsonObjects=new ArrayList<JSONObject>();

        try {

            JSONObject object=new JSONObject(mysortJson);
            JSONArray jsonArray=object.getJSONArray("Data");
            for (int i=0;i<jsonArray.length();i++)
            {
                jsonObjects.add(jsonArray.getJSONObject(i));
            }

            if (sortType.equals("1"))
            {
                Collections.sort(jsonObjects, new Comparator<JSONObject>() {
                    @Override
                    public int compare(JSONObject a, JSONObject b) {
                        String val1=new String();
                        String val2=new String();

                        try {

                            val1=(String) a.get("Name");
//                        Log.e("Value1",""+val1);
                            val2=(String) b.get("Name");
//                        Log.e("Value2",""+val2);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        return val1.compareTo(val2);
                    }
                });

                for (int j=0;j<jsonObjects.size();j++)
                {
                    jsonArray.put(jsonObjects.get(j));
                }
                voter_id=new String[jsonArray.length()];
                boothname_id=new String[jsonArray.length()];
                voter_name=new String[jsonArray.length()];
                voter_f_m_h_name=new String[jsonArray.length()];
                voter_epic=new String[jsonArray.length()];
                voter_h_no=new String[jsonArray.length()];
                voter_mobile=new String[jsonArray.length()];
                voter_gender=new String[jsonArray.length()];
                voter_age=new String[jsonArray.length()];
                get_voter_dob=new String[jsonArray.length()];
                get_marriage_anniv=new String[jsonArray.length()];
                get_voter_caste_id=new String[jsonArray.length()];
                get_voter_status_id=new String[jsonArray.length()];
                get_voter_social_status_id=new String[jsonArray.length()];

                for (int c=0;c<jsonArray.length();c++)
                {
                    JSONObject fetchVoter = jsonArray.getJSONObject(c);
                    voter_id[c] = fetchVoter.getString(KEY_VOTER_ID);
                    boothname_id[c] = fetchVoter.getString(KEY_BOOTHNAME_ID);
                    voter_name[c] = fetchVoter.getString(KEY_SEARCH_NAME);
                    voter_f_m_h_name[c] = fetchVoter.getString(KEY_SEARCH_F_H_M_NAME);
                    voter_epic[c] = fetchVoter.getString(KEY_SEARCH_EPIC_NAME);
                    voter_h_no[c] = fetchVoter.getString(KEY_SEARCH_HOUSE_NUMBER);
                    voter_mobile[c]=fetchVoter.getString(KEY_SEARCH_MOBILE);
                    voter_gender[c]=fetchVoter.getString(KEY_SEARCH_GENDER);
                    voter_age[c]=fetchVoter.getString(KEY_SEARCH_AGE);
                    get_voter_dob[c]=fetchVoter.getString(KEY_SEARCH_DOB);
                    get_marriage_anniv[c]=fetchVoter.getString(KEY_MARRIEGE_ANNIV);
                    get_voter_caste_id[c]=fetchVoter.getString(KEY_GET_CASTE_ID);
                    get_voter_status_id[c]=fetchVoter.getString(KEY_GET_STATUS_ID);
                    get_voter_social_status_id[c]=fetchVoter.getString(KEY_GET_SOCIAL_STATUS);



                }
                CustomSearchList adapter=new CustomSearchList(this,ParseJsonData.voter_id,ParseJsonData.boothname_id,ParseJsonData.voter_name,ParseJsonData.voter_f_m_h_name,ParseJsonData.voter_epic,ParseJsonData.voter_h_no,ParseJsonData.voter_mobile,ParseJsonData.voter_age,ParseJsonData.voter_gender,ParseJsonData.get_marriage_anniv,ParseJsonData.get_voter_dob,ParseJsonData.get_voter_caste_id,ParseJsonData.get_voter_status_id,ParseJsonData.get_voter_social_status_id);
                searchlist.setAdapter(adapter);
                adapter.notifyDataSetChanged();



            }
            else if (sortType.equals("2"))
            {
                Collections.sort(jsonObjects, new Comparator<JSONObject>() {
                    @Override
                    public int compare(JSONObject a, JSONObject b) {
                        String val1=new String();
                        String val2=new String();

                        try {

                            val1=(String) a.get("FName");
//                        Log.e("Value1",""+val1);
                            val2=(String) b.get("FName");
//                        Log.e("Value2",""+val2);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        return val1.compareTo(val2);
                    }
                });

                for (int j=0;j<jsonObjects.size();j++)
                {
                    jsonArray.put(jsonObjects.get(j));
                }
                voter_id=new String[jsonArray.length()];
                boothname_id=new String[jsonArray.length()];
                voter_name=new String[jsonArray.length()];
                voter_f_m_h_name=new String[jsonArray.length()];
                voter_epic=new String[jsonArray.length()];
                voter_h_no=new String[jsonArray.length()];
                voter_mobile=new String[jsonArray.length()];
                voter_gender=new String[jsonArray.length()];
                voter_age=new String[jsonArray.length()];
                get_voter_dob=new String[jsonArray.length()];
                get_marriage_anniv=new String[jsonArray.length()];
                get_voter_caste_id=new String[jsonArray.length()];
                get_voter_status_id=new String[jsonArray.length()];
                get_voter_social_status_id=new String[jsonArray.length()];

                for (int c=0;c<jsonArray.length();c++)
                {
                    JSONObject fetchVoter = jsonArray.getJSONObject(c);
                    voter_id[c] = fetchVoter.getString(KEY_VOTER_ID);
                    boothname_id[c] = fetchVoter.getString(KEY_BOOTHNAME_ID);
                    voter_name[c] = fetchVoter.getString(KEY_SEARCH_NAME);
                    voter_f_m_h_name[c] = fetchVoter.getString(KEY_SEARCH_F_H_M_NAME);
                    voter_epic[c] = fetchVoter.getString(KEY_SEARCH_EPIC_NAME);
                    voter_h_no[c] = fetchVoter.getString(KEY_SEARCH_HOUSE_NUMBER);
                    voter_mobile[c]=fetchVoter.getString(KEY_SEARCH_MOBILE);
                    voter_gender[c]=fetchVoter.getString(KEY_SEARCH_GENDER);
                    voter_age[c]=fetchVoter.getString(KEY_SEARCH_AGE);
                    get_voter_dob[c]=fetchVoter.getString(KEY_SEARCH_DOB);
                    get_marriage_anniv[c]=fetchVoter.getString(KEY_MARRIEGE_ANNIV);
                    get_voter_caste_id[c]=fetchVoter.getString(KEY_GET_CASTE_ID);
                    get_voter_status_id[c]=fetchVoter.getString(KEY_GET_STATUS_ID);
                    get_voter_social_status_id[c]=fetchVoter.getString(KEY_GET_SOCIAL_STATUS);



                }
                CustomSearchList adapter=new CustomSearchList(this,ParseJsonData.voter_id,ParseJsonData.boothname_id,ParseJsonData.voter_name,ParseJsonData.voter_f_m_h_name,ParseJsonData.voter_epic,ParseJsonData.voter_h_no,ParseJsonData.voter_mobile,ParseJsonData.voter_age,ParseJsonData.voter_gender,ParseJsonData.get_marriage_anniv,ParseJsonData.get_voter_dob,ParseJsonData.get_voter_caste_id,ParseJsonData.get_voter_status_id,ParseJsonData.get_voter_social_status_id);
                searchlist.setAdapter(adapter);
                adapter.notifyDataSetChanged();

            }
            else if (sortType.equals("3"))
            {
                Collections.sort(jsonObjects, new Comparator<JSONObject>() {
                    @Override
                    public int compare(JSONObject a, JSONObject b) {
                        String val1=new String();
                        String val2=new String();

                        try {

                            val1=(String) a.get("EPIC");
//                        Log.e("Value1",""+val1);
                            val2=(String) b.get("EPIC");
//                        Log.e("Value2",""+val2);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        return val1.compareTo(val2);
                    }
                });

                for (int j=0;j<jsonObjects.size();j++)
                {
                    jsonArray.put(jsonObjects.get(j));
                }
                voter_id=new String[jsonArray.length()];
                boothname_id=new String[jsonArray.length()];
                voter_name=new String[jsonArray.length()];
                voter_f_m_h_name=new String[jsonArray.length()];
                voter_epic=new String[jsonArray.length()];
                voter_h_no=new String[jsonArray.length()];
                voter_mobile=new String[jsonArray.length()];
                voter_gender=new String[jsonArray.length()];
                voter_age=new String[jsonArray.length()];
                get_voter_dob=new String[jsonArray.length()];
                get_marriage_anniv=new String[jsonArray.length()];
                get_voter_caste_id=new String[jsonArray.length()];
                get_voter_status_id=new String[jsonArray.length()];
                get_voter_social_status_id=new String[jsonArray.length()];

                for (int c=0;c<jsonArray.length();c++)
                {
                    JSONObject fetchVoter = jsonArray.getJSONObject(c);
                    voter_id[c] = fetchVoter.getString(KEY_VOTER_ID);
                    boothname_id[c] = fetchVoter.getString(KEY_BOOTHNAME_ID);
                    voter_name[c] = fetchVoter.getString(KEY_SEARCH_NAME);
                    voter_f_m_h_name[c] = fetchVoter.getString(KEY_SEARCH_F_H_M_NAME);
                    voter_epic[c] = fetchVoter.getString(KEY_SEARCH_EPIC_NAME);
                    voter_h_no[c] = fetchVoter.getString(KEY_SEARCH_HOUSE_NUMBER);
                    voter_mobile[c]=fetchVoter.getString(KEY_SEARCH_MOBILE);
                    voter_gender[c]=fetchVoter.getString(KEY_SEARCH_GENDER);
                    voter_age[c]=fetchVoter.getString(KEY_SEARCH_AGE);
                    get_voter_dob[c]=fetchVoter.getString(KEY_SEARCH_DOB);
                    get_marriage_anniv[c]=fetchVoter.getString(KEY_MARRIEGE_ANNIV);
                    get_voter_caste_id[c]=fetchVoter.getString(KEY_GET_CASTE_ID);
                    get_voter_status_id[c]=fetchVoter.getString(KEY_GET_STATUS_ID);
                    get_voter_social_status_id[c]=fetchVoter.getString(KEY_GET_SOCIAL_STATUS);



                }
                CustomSearchList adapter=new CustomSearchList(this,ParseJsonData.voter_id,ParseJsonData.boothname_id,ParseJsonData.voter_name,ParseJsonData.voter_f_m_h_name,ParseJsonData.voter_epic,ParseJsonData.voter_h_no,ParseJsonData.voter_mobile,ParseJsonData.voter_age,ParseJsonData.voter_gender,ParseJsonData.get_marriage_anniv,ParseJsonData.get_voter_dob,ParseJsonData.get_voter_caste_id,ParseJsonData.get_voter_status_id,ParseJsonData.get_voter_social_status_id);
                searchlist.setAdapter(adapter);
                adapter.notifyDataSetChanged();


            }
            else if (sortType.equals(4))
            {
                Collections.sort(jsonObjects, new Comparator<JSONObject>() {
                    @Override
                    public int compare(JSONObject a, JSONObject b) {
                        String val1=new String();
                        String val2=new String();

                        try {

                            val1=(String) a.get("HouseNo");
//                        Log.e("Value1",""+val1);
                            val2=(String) b.get("HouseNo");
//                        Log.e("Value2",""+val2);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        return val1.compareTo(val2);
                    }
                });

                for (int j=0;j<jsonObjects.size();j++)
                {
                    jsonArray.put(jsonObjects.get(j));
                }
                voter_id=new String[jsonArray.length()];
                boothname_id=new String[jsonArray.length()];
                voter_name=new String[jsonArray.length()];
                voter_f_m_h_name=new String[jsonArray.length()];
                voter_epic=new String[jsonArray.length()];
                voter_h_no=new String[jsonArray.length()];
                voter_mobile=new String[jsonArray.length()];
                voter_gender=new String[jsonArray.length()];
                voter_age=new String[jsonArray.length()];
                get_voter_dob=new String[jsonArray.length()];
                get_marriage_anniv=new String[jsonArray.length()];
                get_voter_caste_id=new String[jsonArray.length()];
                get_voter_status_id=new String[jsonArray.length()];
                get_voter_social_status_id=new String[jsonArray.length()];

                for (int c=0;c<jsonArray.length();c++)
                {
                    JSONObject fetchVoter = jsonArray.getJSONObject(c);
                    voter_id[c] = fetchVoter.getString(KEY_VOTER_ID);
                    boothname_id[c] = fetchVoter.getString(KEY_BOOTHNAME_ID);
                    voter_name[c] = fetchVoter.getString(KEY_SEARCH_NAME);
                    voter_f_m_h_name[c] = fetchVoter.getString(KEY_SEARCH_F_H_M_NAME);
                    voter_epic[c] = fetchVoter.getString(KEY_SEARCH_EPIC_NAME);
                    voter_h_no[c] = fetchVoter.getString(KEY_SEARCH_HOUSE_NUMBER);
                    voter_mobile[c]=fetchVoter.getString(KEY_SEARCH_MOBILE);
                    voter_gender[c]=fetchVoter.getString(KEY_SEARCH_GENDER);
                    voter_age[c]=fetchVoter.getString(KEY_SEARCH_AGE);
                    get_voter_dob[c]=fetchVoter.getString(KEY_SEARCH_DOB);
                    get_marriage_anniv[c]=fetchVoter.getString(KEY_MARRIEGE_ANNIV);
                    get_voter_caste_id[c]=fetchVoter.getString(KEY_GET_CASTE_ID);
                    get_voter_status_id[c]=fetchVoter.getString(KEY_GET_STATUS_ID);
                    get_voter_social_status_id[c]=fetchVoter.getString(KEY_GET_SOCIAL_STATUS);



                }
                CustomSearchList adapter=new CustomSearchList(this,ParseJsonData.voter_id,ParseJsonData.boothname_id,ParseJsonData.voter_name,ParseJsonData.voter_f_m_h_name,ParseJsonData.voter_epic,ParseJsonData.voter_h_no,ParseJsonData.voter_mobile,ParseJsonData.voter_age,ParseJsonData.voter_gender,ParseJsonData.get_marriage_anniv,ParseJsonData.get_voter_dob,ParseJsonData.get_voter_caste_id,ParseJsonData.get_voter_status_id,ParseJsonData.get_voter_social_status_id);
                searchlist.setAdapter(adapter);
                adapter.notifyDataSetChanged();


            }

//            Log.e("Length Array",""+jsonArray.length());
//            Log.e("Length List",""+jsonObjects.size());



        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

I use `adapter.notifyDataSetChanged(); but my custom list not affect the sort. Please suggest me what to do.

6
  • You should not sort ListView or Adapter. You need to pass already sorted array to adapter. Commented Oct 10, 2016 at 6:27
  • Your mean to say first parse array data and then set to adapter... Commented Oct 10, 2016 at 6:28
  • Sort your dataset and call notifyDataSetChanged on the adapter. Commented Oct 10, 2016 at 6:33
  • @K Neeraj Lal can u provide a example for that Commented Oct 10, 2016 at 6:39
  • Use a POJO class, use GSON library to parse the JSON data, Use a Comparator to sort and call notifyDataSetChanged on the adapter to reflect the changes on the UI. Commented Oct 10, 2016 at 7:03

5 Answers 5

1

Before set the data to Listview, you should sort the data using comparator class in java.

you create one Model class with all variable what you want (name,booth_id etc)and store the each object into the ArrayList.

Collections.sort(ArrayListObject,new NameComparator()); 

example

class NameComparator implements Comparator{  
public int compare(Object o1,Object o2){  
YourClassName s1=(YourClassName )o1;  
YourClassName s2=(YourClassName )o2;  

return s1.name.compareTo(s2.name);  
}  
} 
Sign up to request clarification or add additional context in comments.

1 Comment

can u plz provide a example..because i search everywhere ...?/
1

In my case i got homeList(ArrayList of model 'Home') from JSONParser class so befor setAdapter

 Collections.sort(homeList, new Comparator<Home>() {
                    public int compare(Home emp1, Home emp2) {
                        int price1 = emp1.getPrice();
                        int price2 = emp2.getPrice();



                        return Integer.compare(price1, price2);

                    }
                });
    Adapter adapter = new Adapter(getActivity(),homeList);
    listView.setadapter(adapter);

So this will first sort whole arraylist and then we need to setAdapter.

Try this i think this will help you.

3 Comments

thanx for answer let me try..i will let u know
but i did not use get and set in my adapter class
I think you have to use Model with getter setter , that will be less complex , and more reliable for you..
1

Instead of creating one object of data with string array for its fields. Create one object for each element of JsonArray. Create a list for objects from JsonArray. Pass that list in your Custom Adapter. Set the listview adapter as custom adapter.

Now whenever you want to sort the data by any field. Just use Java collections comparator or you can write your own custom comparator.

After that just call notifyDataSetChanged() on the adapter.

Edit -

You can create your model SearchItem like below

class SearchItem {
    private String voterId;
    private String boothNameId;
    private String searchName;
    private String houseNumber;
    private String gender;

    public void setVoterId(String voterId) {
        this.voterId = voterId;
    }

    public String getVoterId() {
        return voterId;
    }

    ......................(Add as many fields you want and their corresponding getter and setter methods)
}

Your Json parse class can be modified like this (You have to populate all the fields of SearchItem class.

class ParseJsonData {

    public static ArrayList<SearchList> parseSearchresult(String jsonResponse) {
        try {
            JSONObject searchData = new JSONObject(jsonResponse);
            JSONArray searchArray=searchData.getJSONArray(SEARCH_VOTER_ARRAY);
            ArrayList<SearchItem> searchList = new ArrayList<SearchItem>();
            for(int index = 0; index < searchArray.length(); index++) {
                JSONObject searchItem = searchArray.get(index);
                SearchItem item = new SearchItem();
                item.setVoterId(searchItem.optString(KEY_VOTER_ID, null);
                item.setBoothNameId(searchItem.optString(KEY_BOOTHNAME_ID, null);
                item.setSearchName(searchItem.optString(KEY_SEARCH_NAME, null);
                item.setHouseNumber(searchItem.optString(KEY_SEARCH_HOUSE_NUMBER, null);
                item.setGender(searchItem.optString(KEY_SEARCH_GENDER, null);
                searchList.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return searchList;
    }
}

Now you can create your own custom adapter as follows

class CustomSearchList extends BaseAdapter {
    ArrayList<SearchItem> mSearchList;
    Context mContext;

    public CustomSearchList(Context context, ArrayList<SearchItem> searchList) {
        mContext = context;
        mSearchList = searchList;
    }

    ..........................(Implement methods for BaseAdapter or other methods you want to write)
}

Here searchList is passed as reference, so any update in that list followed by notify call to adapter will reflect the change.

Your listview class can be modified like this. parseSearchList() method returns an arraylist of all searchlist.

private void getList(String json) {
    ArrayList<SearchItem> searchList = ParseJsonData.parseSearchresult(json);
    CustomSearchList adapter = new CustomSearchList(this, searchList);
    searchlistView.setAdapter(adapter);

    mBtn.setOnClickListener(this, new OnClickListener() {
        @Override
        public void onClick(....) {
            //Suppose when some button click happens for sorting
            Collections.sort(searchList, <yourCustomComparator>);
            notifyDataSetChanged();
        }
    });
}

It would be better if you can make searchList as a Member variable not the local one.

1 Comment

You have to sort already passed arraylist in adapter. Check my example above. This approach may work but is inefficient and not advisable.
1

Yes , i think you have to use model class. Here is example with onPostExecute.

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pd.dismiss();

        try {
            JSONObject object = new JSONObject(s.toString());
            YourModelNameList = new ArrayList<>();

            if (object.getString("status").equalsIgnoreCase("true")) {
                JSONArray array = object.getJSONArray("data");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonProduct = array.getJSONObject(i);
                    YourModelName home = new Home();
                    String special = jsonProduct.getString("special_price");
                    home.setId(jsonProduct.getString("product_id"));
                    home.setName(jsonProduct.getString("product_name"));
                    home.setPrice(jsonProduct.getString("price"));
                    YourModelArrayList.add(home);
                }
            } 
            Collections.sort(YourModelArrayList, new Comparator<YourModelName>() {
                public int compare(YourModelName emp1, YourModelName emp2) {
                    int price1 = emp1.getPrice();
                    int price2 = emp2.getPrice();



                    return Integer.compare(price1, price2);

                }
            });


            adapter = new YourModelArrayListAdapter(getActivity(), YourModelArrayList);
            recyclerView.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } 

Try this if this will help you. Thank you.

1 Comment

no, this will sort your whole arraylist of model class. For ex : you get unsorted(Regular array) into array. now you have to write code for collections.sort method. and that will sort your whole model class arraylist.
1

I have sort the arraylist named ArrayList> addressaray; add response data to arraylist using

JSONObject object = new JSONArray(s.toString());
JSONArray arr1 = new JSONArray(object.getString("data").toString()).getJSONArray(0);
for (int i = 0; i < arr1.length(); i++) {
 JSONObject jsonObject = arr1.getJSONObject(i);
 HashMap<String, String> data = new HashMap<String, String>();
 data.put("id", jsonObject.getString("id"));
 data.put("date_inspected_details", jsonObject.getString("date_inspected_details"));
 addressaray.add(data);
    }
    adapter = new PropertiesEvaluatedFragmentAdapter(getActivity(), addressaray, getActivity().getSupportFragmentManager());
  listView.setAdapter(adapter);
  adapter.notifyDataSetChanged();
}

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String item = parent.getItemAtPosition(position).toString();
 if(addressaray.size() > 0){
                sortListView(item);
                adapter.notifyDataSetChanged();
            }else{
                Toast.makeText(getActivity(), "No Record found.", Toast.LENGTH_SHORT).show();
            }

}

  private void sortListView(String option) {
        switch (option) {
            case "Date":

Collections.sort(addressaray, new Comparator<HashMap<String, String>>() {
                    final static String COMPARE_KEY = "date_inspected_details";

                    @Override
                    public int compare(HashMap<String, String> lhs,
                                       HashMap<String, String> rhs) {
                        String Date1 = lhs.get(COMPARE_KEY);
                        String Date2 = rhs.get(COMPARE_KEY);
                        Log.e("Date1", Date1);
                        Log.e("Date2", Date2);
                        // Do your comparison logic here and retrn accordingly.
                        return Date1.compareTo(Date2);
                    }
                });
  break;
}
}JSONObject object = new JSONArray(s.toString());
JSONArray arr1 = new JSONArray(object.getString("data").toString()).getJSONArray(0);
for (int i = 0; i < arr1.length(); i++) {
 JSONObject jsonObject = arr1.getJSONObject(i);
 HashMap<String, String> data = new HashMap<String, String>();
 data.put("id", jsonObject.getString("id"));
 data.put("date_inspected_details", jsonObject.getString("date_inspected_details"));
 addressaray.add(data);
    }
    adapter = new PropertiesEvaluatedFragmentAdapter(getActivity(), addressaray, getActivity().getSupportFragmentManager());
  listView.setAdapter(adapter);
  adapter.notifyDataSetChanged();
}

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String item = parent.getItemAtPosition(position).toString();
 if(addressaray.size() > 0){
                sortListView(item);
                adapter.notifyDataSetChanged();
            }else{
                Toast.makeText(getActivity(), "No Record found.", Toast.LENGTH_SHORT).show();
            }

}

  private void sortListView(String option) {
        switch (option) {
            case "Date":

Collections.sort(addressaray, new Comparator<HashMap<String, String>>() {
                    final static String COMPARE_KEY = "date_inspected_details";

                    @Override
                    public int compare(HashMap<String, String> lhs,
                                       HashMap<String, String> rhs) {
                        String Date1 = lhs.get(COMPARE_KEY);
                        String Date2 = rhs.get(COMPARE_KEY);
                        Log.e("Date1", Date1);
                        Log.e("Date2", Date2);
                        // Do your comparison logic here and retrn accordingly.
                        return Date1.compareTo(Date2);
                    }
                });
  break;
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.