我正在尝试在我的用户安装应用程序列表中获取已安装的应用程序图标,以获取一个复选框,如下所示:
addCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (addCheckbox.isChecked()){
System.out.println("Checked");
// GET ITEM ICON HERE
}else{
System.out.println("Un-Checked");
}
}});
在我的列表中,我为每个 listView 项目设置了这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<ImageView
android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dip"
android:scaleType="center"
android:contentDescription="@string/desc"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/tvPack"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
<CheckBox
android:id="@+id/addCheckbox"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.3"
android:gravity="center_vertical"
/>
</LinearLayout>
因此,我尝试通过执行此操作获取 listView 项,然后获取图标(就像我在其他班级中所做的那样):
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
但我不知道如何实现它,以便我可以从列表中获取所选项目,以便我可以获取图标(如果我什至需要这样做,或者我可以使用我设置的图标列表视图项目)。
我正在尝试获取图标,以便可以在 gridView 中使用它。
我会以正确的方式解决这个问题吗?否则我该怎么做?(如果您需要查看更多代码,请告诉我!)
进一步说明:
因此,为了获取已安装应用程序的列表,我有一个获取列表信息的类,然后是一个自定义适配器。这是 Utlities.java(我获取信息的部分):
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<?> getInstalledApplication(Context c) {
return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
}
然后我在这里有一个自定义适配器,它根据布局对信息进行排序:AppInfoAdapter.java:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class AppInfoAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
private List<ApplicationInfo> originalListAppInfo;
private Filter filter;
public AppInfoAdapter(Context c, List<ApplicationInfo> listApp, PackageManager pm) {
mContext = c;
this.originalListAppInfo = this.mListAppInfo = listApp;
mPackManager = pm;
}
@Override
public int getCount() {
return mListAppInfo.size();
}
@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final 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);
final CheckBox addCheckbox = (CheckBox)v.findViewById(R.id.addCheckbox);
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.loadLabel(mPackManager));
tvPkgName.setText(entry.packageName);
addCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (addCheckbox.isChecked()){
System.out.println("Checked");
PackageManager pm = mContext.getPackageManager();
Drawable icon = pm.getApplicationIcon(apk.package_name);
Drawable default_icon = pm.getDefaultActivityIcon();
if (icon instanceof BitmapDrawable && default_icon instanceof BitmapDrawable) {
BitmapDrawable icon_bd = (BitmapDrawable)icon;
Bitmap icon_b = icon_bd.getBitmap();
BitmapDrawable default_bd = (BitmapDrawable)pm.getDefaultActivityIcon();
Bitmap default_b = default_bd.getBitmap();
if (icon_b == default_b) {
// It's the default icon
}
}else{
System.out.println("Un-Checked");
}
}});
// return view
return v;
}
所以在这里,我必须修复这些行:
Drawable icon = pm.getApplicationIcon(apk.package_name);
这样我就可以通过包名获取图标。这就是为什么我试图在我的“就像我在其他类(上)中所做的那样”部分中导入和使用,以便我可以获取 listView 项目的位置,然后获取图标。
注意:我尝试过这样的事情:
Drawable icon = pm.getApplicationIcon(AppInfoAdapter.getItem(position).packageName);
但它不起作用,只会导致一堆错误。
新错误:
当我更改这些行时:
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.loadLabel(mPackManager));
对此:
ivAppIcon.setImageDrawable(entry.applicationInfo.loadIcon(mPackManager));
tvAppName.setText(entry.applicationInfo.loadLabel(mPackManager));
我在这里收到一个错误:
Drawable icon = pm
.getApplicationIcon(entry.packageName);
说用try and catch语句包围它,所以我这样做:
Drawable icon = null;
try {
icon = pm
.getApplicationIcon(entry.packageName);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}