排序JSONArray后,自定义列表视图没有改变?

Posted

技术标签:

【中文标题】排序JSONArray后,自定义列表视图没有改变?【英文标题】:After sorting JSONArray,Custom list view not changed? 【发布时间】:2017-02-18 12:26:21 【问题描述】:

我正在对 JSONArray 进行排序并在自定义列表视图中显示它们,但排序后数据在自定义列表视图中没有更改。

这是我的 fab 按钮代码,用于选择要执行的排序:

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


    
);

在 fab 按钮中进行选择后,单击我对 json 数组进行排序。 下面是解析 json 数组的代码:

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

我使用 `adapter.notifyDataSetChanged();但我的自定义列表不会影响排序。 请建议我该怎么做。

【问题讨论】:

您不应该对 ListView 或 Adapter 进行排序。您需要将已排序的数组传递给适配器。 你的意思是先解析数组数据,然后设置为适配器... 对数据集进行排序并在适配器上调用notifyDataSetChanged @K Neeraj Lal 你能举个例子吗 使用 POJO 类,使用 GSON 库解析 JSON 数据,使用 Comparator 排序并在适配器上调用 notifyDataSetChanged 以反映 UI 上的更改。 【参考方案1】:

在将数据设置为Listview之前,您应该使用java中的比较器类对数据进行排序。

您创建一个模型类,其中包含您想要的所有变量(名称、booth_id 等)并将每个对象存储到 ArrayList 中。

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

例子

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

【讨论】:

你能提供一个例子吗..因为我到处搜索...?/【参考方案2】:

在我的例子中,我从 JSONParser 类中得到了 homeList(ArrayList of model 'Home') 所以在 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);

所以这将首先对整个数组列表进行排序,然后我们需要设置适配器。

试试这个,我想这会对你有所帮助。

【讨论】:

谢谢回答让我试试..我会让你知道 但我没有在我的适配器类中使用 get 和 set 我认为您必须将 Model 与 getter setter 一起使用,这将不那么复杂,并且对您来说更可靠..【参考方案3】:

而不是为其字段创建一个带有字符串数组的数据对象。为 JsonArray 的每个元素创建一个对象。 为 JsonArray 中的对象创建一个列表。 在您的自定义适配器中传递该列表。 将 listview 适配器设置为自定义适配器。

现在,只要您想按任何字段对数据进行排序。只需使用 Java 集合比较器,或者您可以编写自己的自定义比较器。

之后只需在适配器上调用 notifyDataSetChanged()。

编辑 -

您可以像下面这样创建模型 SearchItem

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)

您的 Json 解析类可以这样修改(您必须填充 SearchItem 类的所有字段。

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;
    

现在您可以按如下方式创建自己的自定义适配器

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)

这里 searchList 作为引用传递,因此该列表中的任何更新以及对适配器的通知调用都将反映更改。

您的列表视图类可以这样修改。 parseSearchList() 方法返回一个包含所有搜索列表的数组列表。

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

如果您可以将 searchList 作为成员变量而不是本地变量,那就更好了。

【讨论】:

您必须对适配器中已传递的数组列表进行排序。检查我上面的例子。这种方法可能有效,但效率低下,不可取。【参考方案4】:

是的,我认为您必须使用模型类。这是 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();
        
     

如果这对你有帮助,试试这个。谢谢。

【讨论】:

不,这将对模型类的整个数组列表进行排序。例如:您将未排序(常规数组)放入数组中。现在您必须为 collections.sort 方法编写代码。这将对您的整个模型类数组列表进行排序。【参考方案5】:

我已经对名为 ArrayList 的数组列表进行了排序> addressaray; 使用

将响应数据添加到arraylist
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;


【讨论】:

以上是关于排序JSONArray后,自定义列表视图没有改变?的主要内容,如果未能解决你的问题,请参考以下文章

用户按下排序按钮后如何在列表视图中显示排序列表?

基于列表显示中的自定义可调用在 Django Admin 中排序

Android如何对JSONObjects的JSONArray进行排序

将 JSONarray 转换为 ArrayList

通过上下文菜单删除后刷新自定义列表视图

Java中对JSONArray中的对象的某个字段进行排序