如何使用自定义适配器的setTag和getTag

时间:2014-05-12 03:25:30

标签: android listview tags custom-adapter

我被困了,我需要帮助。我尝试使用set并获取Tag,但我无法了解此操作的效果:

  • 我使用列表视图显示加载到扩展适配器的图像
  • 自定义适配器使用imageview_1,textview_1和button_1
  • 为布局充气
  • 在我的主要活动中,我有一个" Public Void OnClickHandler"对于button_1并在布局上配置" android:onClick",所以当点击按钮时它会做一些事情
  • 单击button_1时,我想从该特定视图中获取textview_1中的文本,然后加载不同的图像。 我想使用get和set TAGS来做到这一点,所以我需要使用button_1和imageview_1进行引用。这里是我的剪辑代码。提前谢谢

自定义适配器

@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;
}

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

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
}

3 个答案:

答案 0 :(得分:11)

你的代码有点混乱,所以我给你一个样本

示例标记类

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中的按钮上

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

因为您将视图单击作为函数的参数

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);
}

我认为你明白了,尝试用这个想法实现你的代码

答案 1 :(得分:4)

你非常接近你的答案。只需按照更改完成您的答案

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if ((convertView == null) || (convertView.getTag() == null)) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        convertView.setTag(holder);

        return convertView;
    }

答案 2 :(得分:1)

回答1:

你想做什么:

在此行之上或之后:holder.code.setText(storeItem.getCode());添加以下内容:holder.share.setTag(storeItem.getCode());

并在onClick中:

public void shareOnClickHandler(View v) {
    String code = v.getTag().toString();
    // 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
}

或者

回答2:

如果您执行以下操作,则可能不需要使用setTaggetTag

  1. 转到xml并从我们将在Java中使用它的按钮中删除onclick属性
  2. 使用以下getView

    @覆盖 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.code = (TextView)convertView.findViewById(R.id.code_item_session_text);                  
        holder.share=(ImageButton)convertView.findViewById(R.id.share_item_session_button);
        convertView.setTag(holder);
    // Check if my setTag is ok for button and get the reference to get 
        //text from textview and the reference to webview, then I gonna load a url
    } else {
        holder=(ViewHolder)convertView.getTag();
    }
    
    final StoreDataForBA storeItem= (StoreDataForBA) getItem(position); // final to use inside click
    holder.image.loadUrl(storeItem.getImage());
    holder.code.setText(storeItem.getCode());
    final ViewHolder fh = holder; // it needs to be final to use inside of clicklistener
    holder.share.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            String text = holder.code.getText().toString(); // I hope that this is what you need.
            String text2 = storeItem.getCode(); //use either but I prefer this.
        }
    });
    return convertView;
    }