欢迎来到代码驿站!

Android代码

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

优化SimpleAdapter适配器加载效率的方法

时间:2021-08-26 08:13:59|栏目:Android代码|点击:

在主Activity中:

listview=(ListView)findViewById(R.id.listview);getData();

//为list添加数据overrideSimpleAdapter=new OverrideSimpleAdapter(getContext(),list,R.layout.list_item_layout,       

new String[]{"num","word","translates"},       

new int[]{R.id.tv_num,R.id.tv_word,R.id.tv_translates});

listview.setAdapter(overrideSimpleAdapter);

重写SimpleAdapter:/**
 * Created by KewenC on 2017/1/26.
 */

public class OverrideSimpleAdapter extends SimpleAdapter {
  /**
   * Constructor
   *
   * @param context The context where the View associated with this SimpleAdapter is running
   * @param data   A List of Maps. Each entry in the List corresponds to one row in the list. The
   *         Maps contain the data for each row, and should include all the entries specified in
   *         "from"
   * @param resource Resource identifier of a view layout that defines the views for this list
   *         item. The layout file should include at least those named views defined in "to"
   * @param from   A list of column names that will be added to the Map associated with each
   *         item.
   * @param to    The views that should display column in the "from" parameter. These should all be
   *         TextViews. The first N views in this list are given the values of the first N columns
   */

  private LayoutInflater mInflater;
  private ArrayList<Map<String, Object>> list;
  private int mResource;
  private int[] mTo;
  private String[] mFrom;

  public OverrideSimpleAdapter(Context context, ArrayList<Map<String, Object>> data, int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    this.list=data;
    this.mInflater = LayoutInflater.from(context);
    this.mResource = resource;
    this.mFrom = from;
    this.mTo = to;
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public Object getItem(int position) {
    return list.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    // 判断是否缓存
    if (convertView == null) {
      holder = new ViewHolder();
      // 通过LayoutInflater实例化布局
      convertView = mInflater.inflate(mResource, null);
//      holder.img = (ImageView) convertView.findViewById(R.id.imageView);
      holder.num = (TextView) convertView.findViewById(mTo[0]);
      holder.word = (TextView) convertView.findViewById(mTo[1]);
      holder.translates = (TextView) convertView.findViewById(mTo[2]);
      convertView.setTag(holder);
    } else {
      // 通过tag找到缓存的布局
      holder = (ViewHolder) convertView.getTag();
    }
    // 设置布局中控件要显示的视图
//    holder.img.setBackgroundResource(R.drawable.ic_launcher);
    holder.num.setText(list.get(position).get(mFrom[0]).toString());// mFrom[0]为“num”Key
    holder.word.setText(list.get(position).get(mFrom[1]).toString());
    holder.translates.setText(list.get(position).get(mFrom[2]).toString());
    return convertView;
  }

  public final class ViewHolder {
//    public ImageView img;
    public TextView num;
    public TextView word;
    public TextView translates;
  }
}

上一篇:Android系统添加自己写的工具

栏    目:Android代码

下一篇:android 实现APP中改变头像图片的实例代码

本文标题:优化SimpleAdapter适配器加载效率的方法

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

推荐教程

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

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

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

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

Copyright © 2020 代码驿站 版权所有