安装的应用程序名单按字母顺序排列 [英] Alphabetize List of Installed Apps

查看:127
本文介绍了安装的应用程序名单按字母顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户安装的应用程序的列表,并希望它出来已经按字母顺序排列(不是分隔或类似的只按字母顺序排列已经什么)我环顾四周,但我还没有真正找到这样的事(这已全部与分隔)。我觉得这应该是一个简单的事情,但我一直没能弄明白。这里是我的code:

Utilities.java:

 包com.example.awesomefilebuilderwidget;进口公共类公用事业{/ *
 *获取移动所有已安装的应用程序,并返回一个列表
 * @参数C应用程序的语境
 * @返回安装的应用程序列表
 * /
公共静态列表< ResolveInfo> getInstalledApplications(上下文C){
    意向意图=新意图(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    返回c.getPackageManager()queryIntentActivities(意向,PackageManager.GET_ACTIVITIES)。
}

AppInfoAdapter.java:

 包com.example.awesomefilebuilderwidget; 进口公共类AppInfoAdapter扩展BaseAdapter实现过滤的{
私人语境mContext;
私人列表< ResolveInfo> mListAppInfo;
私人软件包管理系统mPackManager;
私人列表< ResolveInfo> originalListAppInfo;公共AppInfoAdapter(上下文C,列表与LT; ResolveInfo> listApp,
        软件包管理系统时){
    mContext = C;
    this.originalListAppInfo = this.mListAppInfo = listApp;
    mPackManager =时;
    Log.d(AppInfoAdapter,顶);
}@覆盖
公众诠释的getCount(){
    Log.d(AppInfoAdapter,的getCount());
    返回mListAppInfo.size();
}@覆盖
公共对象的getItem(INT位置){
    Log.d(AppInfoAdapter,的getItem);
    返回mListAppInfo.get(位置);
}@覆盖
众长getItemId(INT位置){
    Log.d(AppInfoAdapter,getItemId);
    返回的位置;
}@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
    //获取选定的条目
    最后ResolveInfo进入=(ResolveInfo)mListAppInfo.get(位置);    //参照convertView
    视图V = convertView;    //抬高新的布局,如果空
    如果(V == NULL){
        LayoutInflater吹气= LayoutInflater.from(mContext);
        V = inflater.inflate(R.layout.layout_appinfo,NULL);
        Log.d(AppInfoAdapter,新布局膨胀);
    }    从布局资源//负荷控制
    ImageView的ivAppIcon =(ImageView的)v.findViewById(R.id.ivIcon);
    TextView的tvAppName =(TextView中)v.findViewById(R.id.tvName);
    TextView的tvPkgName =(TextView中)v.findViewById(R.id.tvPack);
    最后复选框addCheckbox =(复选框)V
            .findViewById(R.id.addCheckbox);
    Log.d(AppInfoAdapter,从加载布局资源控制);    //设置要显示的数据
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.activityInfo.loadLabel(mPackManager));
    tvPkgName.setText(entry.activityInfo.packageName);}

Drag_and_Drop_App:

  //创建新的适配器
    适配器=新AppInfoAdapter(这一点,(名单< ResolveInfo>)Utilities.getInstalledApplications(本),getPackageManager());
    //加载应用程序列表
   mListAppInfo =(ListView控件)findViewById(R.id.lvApps);
    //设置适配器列表视图
    mListAppInfo.setAdapter(适配器);

那么,什么将是按字母顺序排列该列表的最佳方式?

更新:

最后的答案:

 公共静态的ArrayList< ResolveInfo> getInstalledApplications(上下文C){
    意向意图=新意图(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    最终软件包管理系统下午= c.getPackageManager();
    ArrayList的< ResolveInfo> APPLIST =(ArrayList的< ResolveInfo>)c.getPackageManager()queryIntentActivities(意向,PackageManager.GET_ACTIVITIES)。
    Collections.sort(APPLIST,新的比较< ResolveInfo>(){
          公众诠释比较(ResolveInfo EMP1,ResolveInfo EMP2){
            返回emp1.loadLabel(PM)的ToString()与compareToIgnoreCase(emp2.​​loadLabel(PM)的ToString());
          }
        });
    返回APPLIST;
}


解决方案

没有测试:

 公共静态的ArrayList< ResolveInfo> getInstalledApplications(上下文C){
    意向意图=新意图(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    ArrayList的< ResolveInfo> APPLIST = c.getPackageManager()queryIntentActivities(意向,PackageManager.GET_ACTIVITIES)。
    Collections.sort(APPLIST,新的比较< ResolveInfo>(){
          公众诠释比较(ResolveInfo EMP1,ResolveInfo EMP2){
            返回emp1.loadLabel(PM)的ToString()与compareToIgnoreCase(emp2.​​loadLabel(PM)的ToString());
          }
        });
    返回APPLIST;

}

I have a list of the users installed applications and want it to come out already alphabetized (not on dividers or anything like that just already alphabetized) I looked around but I haven't really found anything like this (it has all been with dividers). I feel like this should be a simple thing to do but I haven't been able to figure it out. Here is my code:

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

So what would be the best way to alphabetize this list?

UPDATED:

Final answer:

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

解决方案

Not tested:

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;

}

这篇关于安装的应用程序名单按字母顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆