当前位置: 代码迷 >> Android >> android setTag的妙用和The key must be an application-specific resource id 错误
  详细解决方案

android setTag的妙用和The key must be an application-specific resource id 错误

热度:162   发布时间:2016-04-28 06:44:49.0
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,然后设置在这里!!


  相关解决方案