国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

優化SimpleAdapter適配器加載效率的方法

2019-10-23 18:31:12
字體:
來源:轉載
供稿:網友

在主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適配器加載效率的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平和县| 青河县| 章丘市| 绍兴县| 河源市| 铜鼓县| 栾川县| 濮阳市| 临潭县| 民丰县| 乐山市| 南宁市| 贵南县| 黄冈市| 兴化市| 塔河县| 镇坪县| 突泉县| 瑞安市| 金昌市| 甘孜县| 获嘉县| 辉南县| 崇仁县| 沂南县| 连云港市| 绥宁县| 申扎县| 祥云县| 年辖:市辖区| 岳阳市| 美姑县| 阿瓦提县| 长子县| 临沧市| 横山县| 惠水县| 富蕴县| 珠海市| 福安市| 丹江口市|