Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

android - After sorting JSONArray,Custom list view not changed?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);  
}  
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...