我正在尝试使用以下代码获取已安装应用程序图标的列表,但它只显示可绘制路径列表但不显示图标(即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));
}
}
}
答案 0 :(得分:0)
默认情况下,ArrayAdapter
仅在基础数组的每个元素上调用toString
。要使用ImageView
,您还需要编写一个覆盖ArrayAdapter
方法的getView
子类。使用包含ImageView
的布局,然后在其上覆盖getView
调用setImageDrawable
,从数组中传递Drawable
。