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