Android -- setTag的妙用和The key must be an application-specific resource id 异常


tag是view的一个属性,也可以说是view用于存放参数的一个map,对于提高性能和参数传递都有妙用,比如提高listview的性能:


用于缓存item的view

public View getView(final int position, View convertView, ViewGroup parent) {
		ItemViewHolder holder;

		if (convertView == null) {
			holder = new ItemViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.view_item, null);
			holder.timeTextView = (TextView) convertView.findViewById(R.id.text_item_content_time);
			holder.remarkTextView = (TextView) convertView.findViewById(R.id.text_item_content_remark);
			convertView.setTag(holder);
		} else {
			holder = (ItemViewHolder)convertView.getTag();
		}
               if(mMessageListGroup.get(mMessageList.get(position).getGroupId()).isShown()){
                    convertView.setTag(R.id.child_show, true);
               }else{
                   convertView.setTag(R.id.child_show, false);
               }
		
		return convertView;
	}

在上面的代码中用到了tag,如果是一个好说直接setTag即可,如果有多个又怎么办呢?


setTag还有一个带int类型的重载,但是设置final类型的常量或者写死数字都会出现:The key must be an application-specific resource id 异常:


需要在ids.xml文件中定义一个ID,然后设置在这里!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值