欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-01-21 10:35:36
在主activity中: listview=(listview)findviewbyid(r.id.listview);getdata(); //为list...

在主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;
  }
}

以上这篇优化simpleadapter适配器加载效率的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。