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

优化SimpleAdapter适配器加载效率的方法,第1张

概述在主Activity中:listview=(ListView)findViewById(R.id.listview);getData();//为list添加数据overrideSimpleAdapter=newOverrideSimpleAdapter(getContext(),list,R.layout.list_item_layout,newString[]{\"n

在主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适配器加载效率的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的优化SimpleAdapter适配器加载效率的方法全部内容,希望文章能够帮你解决优化SimpleAdapter适配器加载效率的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1146372.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存