最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android编程实现在adapter中进行数据操作的方法
时间:2022-06-25 23:29:41 编辑:袖梨 来源:一聚教程网
代码如下 | 复制代码 |
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; } } |
相关文章
- 王者荣耀S38赛季有什么更新 12-25
- 王者荣耀S38赛季有什么更新 王者荣耀S38赛季更新内容介绍 12-25
- 世界之外12.25有什么更新 世界之外12月25日更新内容介绍 12-25
- 光遇12.25红石碎片在哪里 光遇12月25日红石碎片位置攻略 12-25
- 奇迹暖暖绚光引途第二天怎么玩 绚光引途day2庆祝之舞搭配攻略 12-25
- 无限暖暖拍照打卡位置在哪里 无限暖暖世界巡游位置全攻略 12-25