问题
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