如何按字母顺序对列表视图进行排序

时间:2015-03-03 19:47:23

标签: java android sorting android-listview android-adapter

我一直在关注如何制作自己的Android启动器的本教程here。我现在可以在列表视图中显示手机上所有已安装的应用程序。如何编辑此代码以使列表按字母顺序排列?

    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);
        }
    });
}

3 个答案:

答案 0 :(得分:6)

虽然第一个答案是正确且最明显的方法,但我相信答案需要一个完整的例子:

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());
    }
});

这将按AppDetail字段的字母顺序对label个对象进行排序。

在致电setAdapter()之前执行此操作。

答案 1 :(得分:4)

在将apps List放入ArrayAdapter using Collections.sort()之前对其进行排序。

答案 2 :(得分:2)

Collections.sort(contactList,
                            new Comparator<Map<String, String>>() {

                                @Override
                                public int compare(Map<String, String> o1,
                                        Map<String, String> o2) {
                                    // TODO Auto-generated method stub
                                    String name1 = o1.get("name");
                                    String name2 = o2.get("name");
                                    if (name1 == null)
                                        return -1;
                                    return name1.compareTo(name2);
                                }

                            });

                }