如何获取列表安装的应用程序图标

时间:2013-07-01 07:59:15

标签: android android-arrayadapter android-image

我正在尝试使用以下代码获取已安装应用程序图标的列表,但它只显示可绘制路径列表但不显示图标(即android.graphics.drawable.BitmapDrawable@417530c8),

有人能告诉我我做错了什么。

由于

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView appList = (ListView) findViewById(R.id.appList);

    ArrayList<Drawable> cacheArrayList = new ArrayList<Drawable>();

    ArrayAdapter<Drawable> aa = new ArrayAdapter<Drawable>(this,
            android.R.layout.simple_list_item_1, cacheArrayList);

    appList.setAdapter(aa);

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);
    for (ApplicationInfo packageInfo : packages) {
        // cacheArrayList.add((String) packageInfo.loadLabel(pm));
        cacheArrayList.add(packageInfo.loadIcon(pm));

    }

}

}

1 个答案:

答案 0 :(得分:0)

默认情况下,ArrayAdapter仅在基础数组的每个元素上调用toString。要使用ImageView,您还需要编写一个覆盖ArrayAdapter方法的getView子类。使用包含ImageView的布局,然后在其上覆盖getView调用setImageDrawable,从数组中传递Drawable