GFlam
GFlam

Reputation: 1109

Alphabatize list of installed apps

Hi I followed the below tutorial and successfully listed all of my installed apps in my application.

List all installed apps in style

However it does not list them alphabetically and i can not figure out how to sort them so that they are. Any help with this would be greatly appreciated. I've tried a few things like this

class IgnoreCaseComparator implements Comparator<String> {
            public int compare(String strA, String strB) {
                return strA.compareToIgnoreCase(strB);
            }
        }
        IgnoreCaseComparator icc = new IgnoreCaseComparator();
        java.util.Collections.sort(SomeArrayList,icc);

But can't figure out how to apply it to the app list titles. Thank you for any help with this

===EDIT===

Thank you for the reply I did the following but have an error on sort. The error reads "The method sort(List, Comparator) in the type Collections is not applicable for the arguments (List, ApplicationInfo.DisplayNameComparator)"

   private List<App> loadInstalledApps(boolean includeSysApps) {
      List<App> apps = new ArrayList<App>();

      PackageManager packageManager = getPackageManager();

      List<PackageInfo> packs = packageManager.getInstalledPackages(0); 

      for(int i=0; i < packs.size(); i++) {
         PackageInfo p = packs.get(i);
         ApplicationInfo a = p.applicationInfo;
         if ((!includeSysApps) && ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1)) {
            continue;
         }
         App app = new App();
         app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
         app.setPackageName(p.packageName);
         app.setVersionName(p.versionName);
         app.setVersionCode(p.versionCode);
         CharSequence description = p.applicationInfo.loadDescription(packageManager);
         app.setDescription(description != null ? description.toString() : "");
         apps.add(app);
      }
      Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));
      return apps;
   }

Upvotes: 9

Views: 3947

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

When you query Android to get the list of installed applications, you will get a List<ApplicationInfo>. Android supplies an ApplicationInfo.DisplayNameComparator for those:

Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(pm)); 

(where pm is an instance of PackageManager).

Upvotes: 15

Related Questions