Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a listview in alphabetical order

I've been following this tutorial here on how to make my own Android launcher. I can currently display all the installed apps on my phone in a listview. How can I edit this code so that the list is in alphabetical order?

    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apps_list);

        loadApps();
        loadListView();
        addClickListener();
    }

private PackageManager manager;
private List<AppDetail> apps; 
private void loadApps(){
    manager = getPackageManager();
    apps = new ArrayList<AppDetail>();

    Intent i = new Intent(Intent.ACTION_MAIN, null);
    i.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0);
    for(ResolveInfo ri:availableActivities){
        AppDetail app = new AppDetail();
        app.label = ri.loadLabel(manager);
        app.name = ri.activityInfo.packageName;
        app.icon = ri.activityInfo.loadIcon(manager);
        apps.add(app);
    }
}

private ListView list;    
private void loadListView(){
    list = (ListView)findViewById(R.id.apps_list);

    ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(this, 
            R.layout.list_item, 
            apps) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
            appIcon.setImageDrawable(apps.get(position).icon);

            TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
            appLabel.setText(apps.get(position).label);

            TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
            appName.setText(apps.get(position).name);

            return convertView;
        }
    };

    list.setAdapter(adapter);           
}

private void addClickListener(){        
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> av, View v, int pos,
                long id) {
            Intent i = manager.getLaunchIntentForPackage(apps.get(pos).name.toString());
            AppsListActivity.this.startActivity(i);
        }
    });
}
like image 817
user1353517 Avatar asked Dec 12 '22 00:12

user1353517


2 Answers

While the first answer is the correct and most obvious way to do it, I believe an answer requires a full example:

Collections.sort(apps, new Comparator<AppDetail>() {

    /* This comparator will sort AppDetail objects alphabetically. */

    @Override
    public int compare(AppDetail a1, AppDetail a2) {

        // String implements Comparable
        return (a1.label.toString()).compareTo(a2.label.toString());
    }
});

This will sort the AppDetail objects alphabetically by their label field.

Do this before calling setAdapter().

like image 144
Y.S Avatar answered Dec 28 '22 07:12

Y.S


Sort your apps List before putting it in the ArrayAdapter, using Collections.sort().

like image 42
CommonsWare Avatar answered Dec 28 '22 07:12

CommonsWare