Sort Android apps alphabetically?

ⅰ亾dé卋堺 提交于 2019-12-22 13:53:47

问题


I have the following code:

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


回答1:


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.




回答2:


Android provides an ApplicationInfo.DisplayNameComparator which can be used for sorting of

List<ApplicationInfo>

Check out: Alphabatize list of installed apps



来源:https://stackoverflow.com/questions/16924409/sort-android-apps-alphabetically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!