欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android编程实现在adapter中进行数据操作的方法

时间:2021-07-07 08:53:24|栏目:Android代码|点击:

本文实例讲述了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程序设计有所帮助。

上一篇:Android 优化之卡顿优化的实现

栏    目:Android代码

下一篇:Android studio 切换flutterSDK之后报错及解决办法(推荐)

本文标题:Android编程实现在adapter中进行数据操作的方法

本文地址:http://www.codeinn.net/misctech/154042.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有