Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
   }
like image 1000
GFlam Avatar asked Nov 17 '11 21:11

GFlam


People also ask

How do I automatically arrange apps on Android?

Automatically Organize Your Apps Drawer To do this, tap the three-dot icon at the upper-right of the screen and tap Clean up pages. This will wipe out all empty space throughout your apps drawer pages. Next, tap the three-dot icon again and this time tap Sort, and then tap Alphabetical order.


1 Answers

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

like image 96
CommonsWare Avatar answered Sep 21 '22 18:09

CommonsWare