Pregunta

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());
              }
            });
¿Fue útil?

Solución

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top