Android项目中如何使用adapter对数据进行处理

本篇文章给大家分享的是有关Android项目中如何使用adapter对数据进行处理,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站是一家专注于网站设计、做网站与策划设计,武城网站建设哪家好?创新互联建站做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:武城等地区。武城做网站价格咨询:13518219792

具体如下:

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 apps;
  private ArrayList 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) GetAllAppInfo.getAllapps(context);
    utils = new DBUtils(context);
    allApkBeans = (ArrayList) 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项目中如何使用adapter对数据进行处理,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


分享名称:Android项目中如何使用adapter对数据进行处理
分享网址:http://scyanting.com/article/jcshsc.html