Android编程实现在adapter中进行数据操作的方法
程序员文章站
2023-12-17 09:52:22
本文实例讲述了android编程实现在adapter中进行数据操作的方法。分享给大家供大家参考,具体如下:
package com.cvte.apkclassif...
本文实例讲述了android编程实现在adapter中进行数据操作的方法。分享给大家供大家参考,具体如下:
package com.cvte.apkclassify; import java.util.arraylist; import android.content.context; import android.content.pm.applicationinfo; import android.content.pm.packageinfo; import android.content.pm.packagemanager; import android.content.pm.packagemanager.namenotfoundexception; import android.graphics.drawable.drawable; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; public class itemadapter extends baseadapter { private context context; private layoutinflater inflater; private packagemanager pmanager ; private arraylist<packageinfo> apps; private arraylist<apkbean> allapkbeans; private apkbean bean = new apkbean(); private dbutils utils ; private applicationinfo appinfo; private static int ll = 1; public itemadapter(context c) { super(); this.context = c; pmanager = context.getpackagemanager(); apps = (arraylist<packageinfo>) getallappinfo.getallapps(context); utils = new dbutils(context); allapkbeans = (arraylist<apkbean>) utils.getallrecords(); for (int i = 0; i < apps.size(); i++) { bean = new apkbean(); bean.setapkname(pmanager.getapplicationlabel(apps.get(i).applicationinfo).tostring()); bean.setkind(0); bean.setpackagename(apps.get(i).applicationinfo.packagename); utils.insert(bean); } } @override public int getcount() { // todo auto-generated method stub return apps.size(); } @override public object getitem(int position) { // todo auto-generated method stub return apps.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view view, viewgroup parent) { viewholder holder = null; inflater = layoutinflater.from(context); if (view==null) { holder = new viewholder(); view = inflater.inflate(r.layout.listitem, null); holder.apkimage = (imageview)view.findviewbyid(r.id.apkimage); holder.apkname =(textview)view.findviewbyid(r.id.apkname); holder.result = (textview)view.findviewbyid(r.id.result); view.settag(holder); }else{ holder=(viewholder)view.gettag(); } if (allapkbeans==null||allapkbeans.size()==0) { packageinfo pinfo = apps.get(position); string name = pmanager.getapplicationlabel(pinfo.applicationinfo).tostring(); holder.apkimage.setbackgrounddrawable(pmanager.getapplicationicon(pinfo.applicationinfo)); holder.apkname.settext(name); holder.result.settext("未分类"); }else{ apkbean bean = allapkbeans.get(position); holder.apkimage.setbackgrounddrawable( geticonfrompackagename(bean.getpackagename())); holder.apkname.settext(bean.getapkname()); if (bean.getkind()!=0) { holder.result.settext("已经分类"); }else{ holder.result.settext("未分类"); } } return view; } class viewholder{ public imageview apkimage; public textview apkname; public textview result; } private drawable geticonfrompackagename(string name){ drawable image = null ; try { appinfo = pmanager.getapplicationinfo(name, packagemanager.get_meta_data); image = pmanager.getapplicationicon(appinfo); } catch (namenotfoundexception e) { log.i("no packagename","no packagename"); } return image; } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android数据库操作技巧总结》、《android编程之activity操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》、《android视图view技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。