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

作者:jingxian 时间:2022-03-10 20:33:32 

在主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,适配器,加载
0
投稿

猜你喜欢

  • SpringBoot整合mybatis常见问题(小结)

    2023-07-23 09:50:12
  • 基于JSON实现传输byte数组过程解析

    2021-11-23 05:07:21
  • 常见的排序算法,一篇就够了

    2022-06-16 06:56:34
  • 浅谈Java如何实现一个基于LRU时间复杂度为O(1)的缓存

    2022-02-02 08:35:36
  • C#实现redis读写的方法

    2023-07-13 16:21:35
  • Java手写线程池的实现方法

    2023-10-30 12:50:03
  • 利用java实现中奖概率详情

    2021-08-10 06:56:58
  • android之listview悬浮topBar效果

    2022-12-24 23:29:58
  • 举例分析Python中设计模式之外观模式的运用

    2021-11-24 01:54:48
  • 微服务间调用Retrofit在Spring Cloud Alibaba中的使用

    2022-09-29 23:13:42
  • 详解Flutter桌面应用如何进行多分辨率适配

    2023-06-17 07:14:59
  • 进度条ProgressBar及ProgressDialog(实例)

    2021-09-24 01:07:42
  • C#仿密保卡功能的简单实现代码

    2022-01-17 04:51:12
  • Java运算符从见过到掌握上

    2022-09-08 02:12:43
  • java三个环境变量配置简单教程

    2023-11-28 20:45:30
  • java模拟实现双向链表

    2022-06-05 09:16:16
  • C# 调用Delphi dll 实例代码

    2023-06-12 22:46:06
  • Swagger2配置方式(解决404报错)

    2022-08-30 17:21:13
  • Android实现View滑动效果的6种方法

    2023-01-29 15:12:19
  • 深入理解Java8新特性之接口中的默认方法和静态方法

    2023-11-24 01:44:25
  • asp之家 软件编程 m.aspxhome.com