一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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 ;

  privateArrayListapps;

  privateArrayListallApkBeans;

  privateApkBean bean =newApkBean();

  privateDBUtils utils ;

  privateApplicationInfo appinfo;

  privatestaticintll =1;

  publicItemAdapter(Context c) {

    super();

    this.context = c;

    pManager = context.getPackageManager();

    apps = (ArrayList) GetAllAppInfo.getAllapps(context);

    utils =newDBUtils(context);

    allApkBeans = (ArrayList) 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() {

    // 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;

  }

}

 

热门栏目