作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试让这个 ListView 正常工作,并且我正在尝试按应用程序名称而不是包名称的字母顺序对列表进行排序。
主 Activity .java
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// set adapter to list view
mListAppInfo.setAdapter(adapter);
AppInfoAdapter.java
public AppInfoAdapter(Context c, List list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
ApplicationInfo entry = (ApplicationInfo) 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);
}
// 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);
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.loadLabel(mPackManager));
tvPkgName.setText(entry.packageName);
// return view
return v;
}
最佳答案
修改您的 Utilities.getInstalledApplication(this)
以使用 Collections.sort()
:
public static List<ApplicationInfo> getInstalledApplication(Context context) {
PackageManager packageManager = context.getPackageManager();
List<ApplicationInfo> apps = packageManager.getInstalledApplications(0);
Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));
return apps;
}
关于android - 按应用程序名称而不是包名称的字母顺序对 ListView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11617627/
我是一名优秀的程序员,十分优秀!