view.getTag(key)在android中返回null

时间:2016-03-28 15:06:02

标签: android gridview view

我在我的应用中使用了以下代码。 View.getTag在getView方法之外返回null值。

public View getView(int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
        mHolder = new GridViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_view_item, parent, false);
        mHolder.overflowButton.setOnClickListener(overflowClickListener);
        mHolder.titleText = (TextView) convertView.findViewById(R.id.gridViewTitleText);
        convertView.setTag(mHolder);
    }
    else
    {
        mHolder = (GridViewHolder) convertView.getTag();
    }
    titleText = "tagsvalues";
    convertView.setTag("titleText", titleText);
    mHolder.titleText.setText(titleText);
    return convertView;
}

/**
 * Click listener for overflow button.
 */
private OnClickListener overflowClickListener = new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        if(v.getTag("titleText") == null)
            Log.e("mName", "Null value returns by above getTag");
    }
};

请检查并指导我解决此问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您正在mHolder.overflowButton上设置点击监听器。这个overflowButton没有任何标签。请注意,您要在convertView.setTag("titleText", titleText);上设置标记,而不是overflowButton

修复:mHolder.overflowButton.setTag("titleText", titleText);