Android Development Wonderful Use of setTag and Anomaly of The key must be an application specific resource id

  • 2021-07-22 11:15:24
  • OfStack

tag is an attribute of view, or an map used by view to store parameters, which is useful for improving performance and parameter transfer, such as improving the performance of listview:

view for caching item

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 is used in the above code. If it is a direct setTag, what should I do if there are multiple?

setTag also has an overload of type int, but setting a constant of type final or writing a dead number will occur:

The key must be an application-specific resource id Exception:

You need to define an ID in the ids. xml file and set it here! !


Related articles: