转自:http://www.cnblogs.com/thu539/archive/2012/02/01/2334455.html
最近在做一个扫描Android系统内已安装程序列表的小功能,需要将已安装程序信息读出来,找出其图标,并保存到一个List<Map<String,Object>>中。方法如下:
public void fetch_installed_apps(){ List<PackageInfo> packages=getPackageManager().getInstalledPackages(0); list=new ArrayList<Map<String,Object>>(packages.size()); for(int i=0;i<packages.size();i++){ Map<String,Object> map=new HashMap<String,Object>(); PackageInfo app=packages.get(i); if((app.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)==ApplicationInfo.FLAG_SYSTEM){//过滤掉系统程序 Log.d("System software","We met System "+i+"th software: "+app.packageName); continue; } String packageName=app.packageName; String label=""; try{ label=app.applicationInfo.loadLabel(getPackageManager()).toString(); } catch(Exception e){ Log.i("Exception",e.toString()); } if(label=="")continue; Drawable icon=null; icon=app.applicationInfo.loadIcon(getPackageManager()).getCurrent();//获得图标的Drawable信息 map.put("img",icon); //压入map中 map.put("name", label); map.put("desc", packageName); list.add(map); //将之加入List中 } Log.d("fetch_installed_apps","Leave fetch!"); return ; }在获得已安装程序后的程序后,网上搜到的方法是使用SimpleAdapter更新ListView里面的数据:
SimpleAdapter notes=new SimpleAdapter(this,list,R.layout.info_row, new String[]{"img","name","desc"},new int[]{R.id.img,R.id.name,R.id.desc}); itemlist.setAdapter(notes);上面代码中的R.id.img是每行中ImageView的ID号,但是用这种方式,只能更新图像保存在res/drawable/下面的图像资源,如上面获取图片的代码改为:
int icon=R.id.image1; map.put("img",icon);否则,直接用SimpleAdapter更新一开始获取的Drawable类型的Icon的图像时,会出现错误:
I/System.out(284): resolveUri failed on bad bitmap uri: android.graphics.drawable.BitmapDrawable@485d7dd0且显示结果如下:
查了很多资料,有说使用异步图像更新的方法,但是此处并非网络环境,还需要下载图片,所以没必要是用那个方式。经过查询资料,发现可以使用SimpleAdapter的ViewBinder方法来实现对Drawable或者Image类型图片的更新。其代码如下:
SimpleAdapter notes=new SimpleAdapter(this,list,R.layout.info_row, new String[]{"img","name","desc"},new int[]{R.id.img,R.id.name,R.id.desc}); itemlist.setAdapter(notes); notes.setViewBinder(new ViewBinder(){ public boolean setViewValue(View view,Object data,String textRepresentation){ if(view instanceof ImageView && data instanceof Drawable){ ImageView iv=(ImageView)view; iv.setImageDrawable((Drawable)data); return true; } else return false; } });用这种方式,成功的实现了对图片的加载,显示效果如下,希望本文对大家有用!
相关推荐
使用SimpleAdapter创建ListView
简单的ListView,适合新手学习
SimpleAdapter实现LIstView的范例[有注解];可直接运行;
Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器-附件资源
ListView使用simpleAdapter填充实现,数据结构是HashMap,对应的我的博客地址是: http://blog.csdn.net/u012320459/article/details/47667869
使用SimpleAdapter创建ListView。。。。另外设置资源分为1,实在是没办法了,没有积分去下载资源学习了,如果实在想要demo,可以去我博客看看,复制粘贴。
使用SimpleAdapter和自定义Adapter分别实现了ListView的填充,自定义的还添加了一个删除的事件。
这个是用SimpleAdapter实现了ListView的数据填充,并实现绚丽的表格效果,表格的样式可以自己随意定义,自己觉得对于初学者用ListView做表格效果是一个不错demo
android 从activity用intent跳转到listactivity并在其中用simpleadapter在listview中显示数据列
系统自带的list单选满足不了需求,本人特写实现了自定义listview实现单选来和大家分享!
Android中尝试气泡短信编程初探实例 ListView使用SimpleAdapter适配器详解 具体参考小魏博客:http://blog.csdn.net/xiaowei_cqu/article/details/7045497
ListView和SimpleAdapter形成导航简单实例。
ListView和SimpleAdapter的简单应用
Android SimpleAdapter 列表(ListView)、表格(GridView)
Android SimpleAdapter的多种显示GridView/ListView
NULL 博文链接:https://aygxylxk.iteye.com/blog/1250203
Android之simpleAdapter
Android Studio —— SimpleAdapter (模仿魅蓝设置界面)。详情查看:https://blog.csdn.net/automoblie0/article/details/122631004
安卓Android源码——(列表之SimpleAdapter适配).zip