简体   繁体   English

Alphabatize已安装应用的列表

[英]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)" 错误读取“类型集合中的方法排序(List,Comparator)不适用于参数(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;
   }

When you query Android to get the list of installed applications, you will get a List<ApplicationInfo> . 当您查询Android以获取已安装的应用程序列表时,您将获得List<ApplicationInfo> Android supplies an ApplicationInfo.DisplayNameComparator for those: Android为这些提供ApplicationInfo.DisplayNameComparator

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

(where pm is an instance of PackageManager ). (其中pmPackageManager一个实例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM