Android sort ListView alphabetically

前端 未结 8 1146
后悔当初
后悔当初 2020-12-05 03:10

So I\'m trying to sort a simple ArrayAdapter for ListView. Here\'s what I\'ve tried:

ListView lv;
String[] months = {
        \"January\",
              


        
相关标签:
8条回答
  • 2020-12-05 03:45

    its simple if you already have another adapter you can just copy paste this code under declaration of adapter

    adapter.sort(new Comparator<String>() {
            @Override
            public int compare(String arg1, String arg0) {
                return arg1.compareTo(arg0);
            }
        });
    
    0 讨论(0)
  • 2020-12-05 03:50

    Rather than add data directly to your adapter, you can add them in Collection. Than sort the collection by using compactor. and finally...

    adapter.addAll(collectionData);
    
    0 讨论(0)
提交回复
热议问题