优化SimpleAdapter适配器加载效率的方法
作者:jingxian 发布时间:2022-03-10 20:33:32
标签:simpleadapter,适配器,加载
在主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;
}
}


猜你喜欢
- (新手写博客,主要是对自己学习的归纳总结。会对很多小细节详解。)单例模式的定义:确保一个类只有一个实例,并提供一个全局访问点。首先实例大家应
- 一:Java创建线程方式继承Thread类或者实现Runnable接口。但是Runnable 的 run() 方法是不带返回值的,那如果我们
- 一、概述Groovy is a multi-faceted language for the Java platform.Apache Gr
- 自定义控件在android中无处不见,自定义控件给了我们很大的方便。比如说,一个视图为imageview ,imagebutton ,tex
- 关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较
- 本文实例为大家分享了XListView实现上拉加载下拉刷新的具体代码,供大家参考,具体内容如下## 导入XListVIew第三方库文件。通过
- 安装完jdk环境后,编写第一个java程序hello.java:public class hello{
- 验证码及它的作用验证码为全自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计算机的公共全自动程序,这个问题可以由计算机生成并评判,但是
- 近几年移动互联网的高速发展,智能手机的使用用户呈现 * 性增长,手机终端上的App 种类繁多,大多数App 都需要与后台系统进行交互,交互的第
- spring boot是个好东西,可以不用容器直接在main方法中启动,而且无需配置文件,方便快速搭建环境。可是当我们要同时启动2个spri
- Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,
- 项目要求基于Broadcast,BroadcastReceiver等与广播相关的知识实现简单的音乐播放功能,包括音乐的播放、暂停、切换、进度
- eureka获取服务ip和端口号进行Http调用我告诉你们为啥我要先从eureka首先获取 goods的服务ip, 在用ip的方式使用htt
- 本文实例为大家分享了unity通过Mesh网格绘制球体的具体代码,供大家参考,具体内容如下接着上一篇文章说:球体public class 球
- 根据使用泛型位置的不同可以分为:声明侧泛型、使用侧泛型。声明侧的泛型信息被记录在Class文件的Constant pool中以Signatu
- 本文实例讲述了Java自定义标签用法。分享给大家供大家参考,具体如下:简单例子实现一个标签分为两步:(1)继承SimpleTagSuppor
- 当maven需要到的依赖jar包不在本地仓库时, 就需要到远程仓库下载 .这个时候如果mavensetting.xml中配置了镜像 , 而且
- 在搜索引擎的开发中,我们需要对Html进行解析。本文介绍C#解析HTML的两种方法。AD: 在搜索引擎的开发中,我们需要对网页的Html内容
- 在Java中可以使用HttpServer类来实现Http服务器,该类位于com.sun.net包下(rt.jar)。实现代码如下:主程序类p
- 本文汇总了常用的DateTime日期类型格式化显示方法,方便读者在使用的时候参考借鉴一下。具体如下所示:1.绑定时格式化日期方法:<A