문제

I use the following code to get a list of all applications on my Android tablet and sort them alphabetically by name.

But for some reason it doesn't work - it leaves the list unchanged.

//load apps
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        final PackageManager pm = getActivity().getPackageManager();
        final List<ResolveInfo> applist = pm.queryIntentActivities( mainIntent, 0);
//sort
        Collections.sort(applist, new Comparator<ResolveInfo>(){
              public int compare(ResolveInfo emp1, ResolveInfo emp2) {
                return emp1.loadLabel(pm).toString().compareToIgnoreCase(emp2.loadLabel(pm).toString());
              }
            });
도움이 되었습니까?

해결책

Fixed it by changing the applist's type from List to ArrayList

다른 팁

try this

final PackageManager pm = context.getPackageManager();
Collections.sort(listInfoapps, new ResolveInfo.DisplayNameComparator(pm));

adapter.notifyDataSetChanged();

Try to use int to compare. It could be better.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top