博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android:ListView:convertView.setTag()来设置数据
阅读量:7223 次
发布时间:2019-06-29

本文共 2125 字,大约阅读时间需要 7 分钟。

void android.view.View.setTag(Object tag)

public void setTag (Object tag)

Since: API Level 1

Sets the tag associated with this view. A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy. Tags can also be used to store data within a view without resorting to another data structure.

Parameters
an Object to tag the view with
also see
  • getTag()
  • setTag(int, Object)
public View getView(int position, View convertView, ViewGroup parent) {            final int cc = position;            ViewHolder holder = null;            if (convertView == null) {                holder = new ViewHolder();                convertView = mInflater.inflate(R.layout.itemrow, null);                holder.tagcolor = (ImageView) convertView.findViewById(R.id.itemrowiamge);                holder.itemname = (TextView) convertView.findViewById(R.id.itemrowtext);                holder.cb = (CheckBox) convertView.findViewById(R.id.itemrowcb);                convertView.setTag(holder);            } else {                holder = (ViewHolder) convertView.getTag();            }             //获取ViewHolder中所填入的数据            holder.tagcolor.setBackgroundResource((Integer) itemList.get(position).get("IMG"));            holder.itemname.setText((String) itemList.get(position).get("ITEMNAME"));            holder.ckd = (String) itemList.get(position).get("CHECKED");             //单个项目中的CheckBox的按键监听函数            holder.cb.setOnClickListener(new View.OnClickListener() {                public void onClick(View v) {                    setC(cc);//当被按下后调用函数改变状态                }            });            if (holder.ckd.equals("t"))                holder.cb.setChecked(true);            else                holder.cb.setChecked(false);            return convertView;        }

设一个holder类

import android.widget.CheckBox;import android.widget.ImageView;import android.widget.TextView; //ViewHolder类用以储存每一条项目所需的数据public class ViewHolder {    public ImageView tagcolor;//单条项目中的图片    public TextView itemname;//单条项目中的文本    public CheckBox cb;//单条项目中的CheckBox    public String ckd;//单条项目中用以标识CheckBox状态的字符串变量}

即可。

转载地址:http://qezfm.baihongyu.com/

你可能感兴趣的文章
关于SQL镜像配置报错
查看>>
终于找到解决方案了,Qt的Model/View Framework解析
查看>>
线程信息的获取和设置
查看>>
Databricks Scala 编程风格指南
查看>>
Tkinter,label内容随多选框变化
查看>>
PHP开发中的数据类型 ( 第3篇 ) :Heaps
查看>>
网络七层协议
查看>>
4种删除Word空白页的小技巧,都是你需要用到的!
查看>>
单服务器MySQL主从复制实践
查看>>
CentOS 7 root口令恢复
查看>>
| 刘知远:让计算机听懂人话
查看>>
苹果收购初创公司Tueo Health,哮喘监测或将应用到Apple Watch
查看>>
CLR存储过程
查看>>
初级运维(一)
查看>>
C语言字符串常用函数学习(一)
查看>>
Lync Server 2010部署与应用(三)---拓扑生成与发布
查看>>
安全摘记1:关于安全与黑客
查看>>
我的友情链接
查看>>
tbox中vector容器的使用
查看>>
一个简单的PHP笔试题
查看>>