代码如下 | 复制代码 |
packagecom.cvte.apkclassify; importjava.util.ArrayList; importandroid.content.Context; importandroid.content.pm.ApplicationInfo; importandroid.content.pm.PackageInfo; importandroid.content.pm.PackageManager; importandroid.content.pm.PackageManager.NameNotFoundException; importandroid.graphics.drawable.Drawable; importandroid.util.Log; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.ImageView; importandroid.widget.TextView; publicclassItemAdapterextendsBaseAdapter { privateContext context; privateLayoutInflater inflater; privatePackageManager pManager ; privateArrayList privateArrayList privateApkBean bean =newApkBean(); privateDBUtils utils ; privateApplicationInfo appinfo; privatestaticintll =1; publicItemAdapter(Context c) { super(); this.context = c; pManager = context.getPackageManager(); apps = (ArrayList utils =newDBUtils(context); allApkBeans = (ArrayList for(inti =0; i < apps.size(); i++) { bean =newApkBean(); bean.setApkname(pManager.getApplicationLabel(apps.get(i).applicationInfo).toString()); bean.setKind(0); bean.setPackagename(apps.get(i).applicationInfo.packageName); utils.insert(bean); } } @Override publicintgetCount() { // TODO Auto-generated method stub returnapps.size(); } @Override publicObject getItem(intposition) { // TODO Auto-generated method stub returnapps.get(position); } @Override publiclonggetItemId(intposition) { // TODO Auto-generated method stub return0; } @Override publicView getView(intposition, View view, ViewGroup parent) { ViewHolder holder =null; inflater = LayoutInflater.from(context); if(view==null) { holder =newViewHolder(); 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("未分类"); } } returnview; } classViewHolder{ publicImageView apkimage; publicTextView apkname; publicTextView result; } privateDrawable 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"); } returnimage; } } |