从getTag中检索类

时间:2015-06-25 16:49:01

标签: android android-adapter

我在ListView适配器中设置了convertView.setTag(cell);,该适配器包含ListCell类。每当我点击其中一个Listview项目时,我都会尝试使用getTag检索ListCell类。

在这种需要检索整个类的情况下,如何使用getTag?

活动 - OnItemClick

this.GetAllCommentsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                //How would I retrieve the class here with getTag?

                Dialogbox();
            }
    });

private class ListCell {
    private TextView comment;
    private ImageView PostImage;
    private TextView PostTitle;
    private TextView submitdate;
    private int commentID;
    private TextView PostVotes;
    private TextView commentCount;
    private int CurrentVote = -1;
    private ImageView PostUpvote;
    private ImageView userIconBackground;
    private ImageView userIcon;
    private GradientDrawable backgroundGradient;
    private int backgroundColor;
}

2 个答案:

答案 0 :(得分:1)

final Object tag = v.getTag();
if(null != tag && tag instanceOf YourClass){
YourClass instance = (YourClass)tag;
}

您也可以使用:

viewInstance.setTag(viewInstance.getId(), yourTagObject);

然后如果您的视图元素,请按ID获取标记。

答案 1 :(得分:0)

很可能你只是在寻找普通的铸造:

ListCell lc = (ListCell)arg1.getTag();   // where arg1 is your View quoted method