[英]Alphabetize List of Installed Apps
我有一个已安装用户的应用程序列表,希望它按字母顺序显示(不在分隔符上或类似的东西按字母顺序排列)我环顾四周,但我还没有真正找到类似的东西(分隔符都包含了) 。 我觉得这应该是一件简单的事情,但我一直无法弄清楚。 这是我的代码:
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;
}
未经测试:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.