Android编程实现在adapter中进行数据操作的方法
本文实例讲述了Android编程实现在adapter中进行数据操作的方法。分享给大家供大家参考,具体如下:
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{ privateContextcontext; privateLayoutInflaterinflater; privatePackageManagerpManager; privateArrayList<PackageInfo>apps; privateArrayList<ApkBean>allApkBeans; privateApkBeanbean=newApkBean(); privateDBUtilsutils; privateApplicationInfoappinfo; privatestaticintll=1; publicItemAdapter(Contextc){ super(); this.context=c; pManager=context.getPackageManager(); apps=(ArrayList<PackageInfo>)GetAllAppInfo.getAllapps(context); utils=newDBUtils(context); allApkBeans=(ArrayList<ApkBean>)utils.getAllRecords(); 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(){ //TODOAuto-generatedmethodstub returnapps.size(); } @Override publicObjectgetItem(intposition){ //TODOAuto-generatedmethodstub returnapps.get(position); } @Override publiclonggetItemId(intposition){ //TODOAuto-generatedmethodstub return0; } @Override publicViewgetView(intposition,Viewview,ViewGroupparent){ ViewHolderholder=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){ PackageInfopinfo=apps.get(position); Stringname=pManager.getApplicationLabel(pinfo.applicationInfo).toString(); holder.apkimage.setBackgroundDrawable(pManager.getApplicationIcon(pinfo.applicationInfo)); holder.apkname.setText(name); holder.result.setText("未分类"); }else{ ApkBeanbean=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{ publicImageViewapkimage; publicTextViewapkname; publicTextViewresult; } privateDrawablegetIconFromPackageName(Stringname){ Drawableimage=null; try{ appinfo=pManager.getApplicationInfo(name,PackageManager.GET_META_DATA); image=pManager.getApplicationIcon(appinfo); }catch(NameNotFoundExceptione){ Log.i("nopackagename","nopackagename"); } returnimage; } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。