按字母顺序排列 Android 应用程序? [英] Sort Android apps alphabetically?

查看:24
本文介绍了按字母顺序排列 Android 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

packageManager = getPackageManager();
    List<PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
    List<PackageInfo> installedapps = new ArrayList<PackageInfo>();

    for(PackageInfo apps: packageList){
        if(!isSystemPackage(apps)){
            installedapps.add(apps);
        }
    }


   Collections.sort(installedapps, new Comparator<PackageInfo>(){
        public int compare(PackageInfo o1, PackageInfo o2) {
            return o1.packageName.compareTo(o2.packageName);
        }
    });



    apkList = (ListView) findViewById(R.id.listView);
    apkList.setAdapter(new AppInfoAdapter(this, installedapps, packageManager));

installedapps 是设备上除系统应用程序之外的所有应用程序的列表.我唯一想做的就是按字母顺序对它们进行排序,不太清楚如何.

installedapps is a list of all the apps on the device minus the system apps. The only thing I want to do is sort them alphabetically, can't quite figure out how.

推荐答案

据我所知,您希望按名称的字母顺序对 PackageInfo 对象列表进行排序.我会在这里开始.您要做的是为您称为 installedapps 的 ArrayList 创建一个自定义比较器,然后使用 Collections.sort(..) 进行排序.您应该用于排序的 PackageInfo 属性是 p.packageName 这是应用程序的完全限定名称(您可以使用正则表达式来隔离包的最后一部分姓名).您可以找到一个隔离应用程序外观属性的示例 此处.

From what I understand, you want to sort a list of PackageInfo objects alphabetically by name. I would start here. What you want to do is create a custom Comparator for the ArrayList you called installedapps and then sort is using Collections.sort(..). The property of PackageInfo you should use for sorting is p.packageName which is the fully qualified name of the Application (you can use regular expressions to isolate the last piece of the package name). An example of isolating cosmetic properties of apps you can find here.

这篇关于按字母顺序排列 Android 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆