如何将setTag和getTag与自定义适配器一起使用 [英] How to use setTag and getTag with custom adapter

查看:86
本文介绍了如何将setTag和getTag与自定义适配器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困住了,需要帮助.我正在尝试使用set和Get Tag,但是我无法了解该操作的工作原理:

I get stucked and I need help. I'm trying to use set and get Tag but i can't get how it works for this action:

  • 我正在使用列表视图显示加载到扩展适配器的图像
  • 自定义适配器使用imageview_1,textview_1和button_1为布局添加膨胀
  • 在我的主要活动中,我为button_1设置了一个公共无效OnClickHandler",并在布局上配置了"android:onClick",因此当单击按钮时,它会执行某些操作
  • 单击button_1时,我想从该特定视图的textview_1中获取文本,然后加载其他图像. 我想使用get和set TAGS进行此操作,因此我需要使用button_1和imageview_1进行引用.这是我的代码片段.预先谢谢你
  • I'm using list view to show images loaded to extended adapter
  • The custom Adapter inflate a layout with imageview_1, textview_1 and button_1
  • On my principal activity, I have a "Public Void OnClickHandler" for button_1 and was configurated on layout with "android:onClick", so when button is clicked it do something
  • When button_1 is clicked, I want to get the text from textview_1 from that specific view and then load a different image. I want to to this using get and set TAGS, so I need to do the reference with button_1 and imageview_1. here my snipped code. Thank you in advance

自定义适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.itemstartsession, null);
        holder = new ViewHolder();

        holder.image = (WebView)convertView.findViewById(R.id.img_session);
        //holder.image.setTag(position);

        holder.code = (TextView)convertView.findViewById(R.id.code_item_session_text);
        //holder.code.setTag(position);


        holder.share=(ImageButton)convertView.findViewById(R.id.share_item_session_button);
        holder.share.setTag(position);

        convertView.setTag(holder);
    // Check if my setTag is ok for button and get the reference to get 
        //text from textview and the referece to webview, then I gonna load a url
    } else {

        holder=(ViewHolder)convertView.getTag();
    }

    StoreDataForBA storeItem= (StoreDataForBA) getItem(position);
    holder.image.loadUrl(storeItem.getImage());

        holder.code.setText(storeItem.getCode());

return convertView;
}

这是我的数据获取和设置方法,非常简单

This is my getter and setter for data, very easy

public StoreDataForBA( String image, String code) {

    this.setImage(image);
    this.setCode(code);

}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}


public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

我的主要活动被剪掉

public void shareOnClickHandler(View v) {
// plz here i need the code to get the text from textview and also get the 
// reference of the webview, so i can do something like
// StoreDataForBA data = (StoreDataForBA)v.getTag();
// image2.loadUrl("http://image2")..... I'm not sure, thank you
}

推荐答案

您的代码有点混乱,所以我给您一个示例

your code is little bit confusing, so I give you a sample

样品标签类

public class MyTag
{
   String  code;
   String  image;
   String  web_ref;

  public MyTag()
    {
     code=null;
     image=null;
     web_ref=null;
    }

    public MyTag(String cod,String img,String wref)
    {
      code=cod;
      image=img;
      web_ref=wref;
    }

}

您想在单击按钮右侧时获得此值吗?因此,将这个标签类对象作为标签添加到自定义适配器的getView中的按钮上

you want to get this values when clicked on button right ? So put this tag class object as tag on button in getView of your custom adapter

MyTag myTag=new MyTag("code","image","web_ref");
holder.button.setTag(myTag);

自从您单击视图作为函数的参数以来,

since you get the view clicked as argument to the your function

public void shareOnClickHandler(View v) 
{

   myTag=(MyTag)v.getTag();
   text=myTag.code;
   image2.loadUrl("http://"+myTag.image);//..... I'm not sure, thank you
   webview.loadUrl(mytag.web_ref);
}

我认为您有主意,请尝试使用此主意实现代码

I think you get the idea, try to implement your code with this idea

这篇关于如何将setTag和getTag与自定义适配器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆