按字母顺序排列已安装的应用程序列表

时间:2014-04-06 22:15:50

标签: java android android-listview

我有一个用户已安装应用程序的列表,并希望它已经按字母顺序出现(不是分隔线或类似已经按字母顺序排列的任何东西)我环顾四周但我还没有找到这样的东西(它有一直都是分隔线)。我觉得这应该是一件简单的事情,但我还没能弄清楚。这是我的代码:

Utilities.java:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class Utilities {

/*
 * Get all installed application on mobile and return a list
 * @param   c   Context of application
 * @return  list of installed applications
 */
public static List<ResolveInfo> getInstalledApplications(Context c) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    return c.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
}

AppInfoAdapter.java:

package com.example.awesomefilebuilderwidget;

 IMPORTS

public class AppInfoAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<ResolveInfo> mListAppInfo;
private PackageManager mPackManager;
private List<ResolveInfo> originalListAppInfo;

public AppInfoAdapter(Context c, List<ResolveInfo> listApp,
        PackageManager pm) {
    mContext = c;
    this.originalListAppInfo = this.mListAppInfo = listApp;
    mPackManager = pm;
    Log.d("AppInfoAdapter", "top");
}

@Override
public int getCount() {
    Log.d("AppInfoAdapter", "getCount()");
    return mListAppInfo.size();
}

@Override
public Object getItem(int position) {
    Log.d("AppInfoAdapter", "getItem");
    return mListAppInfo.get(position);
}

@Override
public long getItemId(int position) {
    Log.d("AppInfoAdapter", "getItemId");
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // get the selected entry
    final ResolveInfo entry = (ResolveInfo) mListAppInfo.get(position);

    // reference to convertView
    View v = convertView;

    // inflate new layout if null
    if (v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_appinfo, null);
        Log.d("AppInfoAdapter", "New layout inflated");
    }

    // load controls from layout resources
    ImageView ivAppIcon = (ImageView) v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView) v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView) v.findViewById(R.id.tvPack);
    final CheckBox addCheckbox = (CheckBox) v
            .findViewById(R.id.addCheckbox);
    Log.d("AppInfoAdapter", "Controls from layout Resources Loaded");

    // set data to display
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.activityInfo.loadLabel(mPackManager));
    tvPkgName.setText(entry.activityInfo.packageName);

}

Drag_and_Drop_App:

// create new adapter
    adapter = new AppInfoAdapter(this, (List<ResolveInfo>) Utilities.getInstalledApplications(this), getPackageManager());
    // load list application
   mListAppInfo = (ListView)findViewById(R.id.lvApps);
    // set adapter to list view
    mListAppInfo.setAdapter(adapter);

那么将此列表按字母顺序排列的最佳方法是什么?

更新:

最终答案:

    public static ArrayList<ResolveInfo> getInstalledApplications(Context c) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    final PackageManager pm = c.getPackageManager();
    ArrayList<ResolveInfo> applist = (ArrayList<ResolveInfo>) c.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
    Collections.sort(applist, new Comparator<ResolveInfo>(){
          public int compare(ResolveInfo emp1, ResolveInfo emp2) {
            return emp1.loadLabel(pm).toString().compareToIgnoreCase(emp2.loadLabel(pm).toString());
          }
        });
    return applist;
}

1 个答案:

答案 0 :(得分:0)

未经测试:

public static ArrayList<ResolveInfo> getInstalledApplications(Context c) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    ArrayList<ResolveInfo> applist = c.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
    Collections.sort(applist, new Comparator<ResolveInfo>(){
          public int compare(ResolveInfo emp1, ResolveInfo emp2) {
            return emp1.loadLabel(pm).toString().compareToIgnoreCase(emp2.loadLabel(pm).toString());
          }
        });
    return applist;

}