How to sort groups and children Expandable listview in Android?

前端 未结 3 2054
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 10:26

I have two tables in mysql which bind \'Categories\' and \'products\' through ids \'id_catprods\' and \'ID_Cat\'. So far so good, I\'m working on Android with \"ExpandableListVi

3条回答
  •  猫巷女王i
    2021-01-22 11:03

    Use

     Collections.sort(arraylist); 
    

    or

    list.sort(String::compareToIgnoreCase);
    

    This will sort your arraylist and If you have Expandable list you have to use

            Collections.sort(arraylist, new Comparator(){
                @Override
                public int compare(Modelclass o1, Modelclass o2) {
    
                    Collections.sort(o1.getsubarraylist(), new Comparator(){
                        @Override
                        public int compare(Modelclass o1, Modelclass o2) {
                        return o1.getName().compareToIgnoreCase(o2.getName());
                           }
                        });
    
                   Collections.sort(o2.getsubarraylist(), new Comparator(){
                        @Override
                        public int compare(Modelclass o1, Modelclass o2) {
                        return o1.getName().compareToIgnoreCase(o2.getName());
                           }
                        });
    
                 return o1.getName().compareToIgnoreCase(o2.getName()); 
    // *This Sorts The Group's if you don't wanna sort group simply return 0*;
              }
            });
    

    NOTE:- o1.getsubarraylist() & o2.getsubarraylist() is Child list's of a expandable list, Just add inner Collections.sort() for sorting expandable list's

    or if you have Simple String arraylist

             Collections.sort(arraylist, new Comparator(){
                @Override
                public int compare(String o1, String o2) {
                 return o1.compareToIgnoreCase(o2);
                  }
               });
    

    I hope this helps someone in need....

提交回复
热议问题