我有以下代码:
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 是设备上除系统应用程序之外的所有应用程序的列表。我唯一想做的就是按字母顺序对它们进行排序,但不太清楚如何排序。
最佳答案
据我了解,您想按名称的字母顺序对 PackageInfo
对象列表进行排序。我会开始 here .您想要做的是为您称为 installedapps
的 ArrayList 创建一个自定义比较器,然后使用 Collections.sort(..)
进行排序。您应该用于排序的 PackageInfo
属性是 p.packageName
,它是应用程序的完全限定名称(您可以使用正则表达式来隔离包的最后一 block 姓名)。您可以找到隔离应用程序外观属性的示例 here .
关于android - 按字母顺序对 Android 应用程序进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16924409/