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.