How to use setTag and getTag with custom adapter
Asked Answered
A

3

13

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:

  • 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

The custom Adapter

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

My principal Activity snipped

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
}
Aarau answered 12/5, 2014 at 3:25 Comment(7)
why you don't have to call click listener of button_1 in adapter class?its too easy rather then ur method....Forswear
@Forswear Because I have to do more complicated actions in that button, I want to know how to use properly that tags, but I guess if I use inside adapter is the same code.. u.uAarau
What's the problem, what was the expected behaviour and what happened instead?Germen
@Shereef - I need to know what's the code in shareOnClickListener to get the text from Convertview using "getTag" and also get the reference from WebView.. need help!Aarau
Could you please explain a bit moreGermen
@Shereef.. Ok.. I have a list view which is using a custom adapter with 3 views : Textview, Webview and ButtonView, so in my principal activity (shareOnClickListener) I want to add actions to this button, first get the text from textview from this convertview on customadapter, I know I can get this text using getTag, i need that code, and in same time I need to load a new Image on Webview , hope this help.. tyAarau
Answered, please checkGermen
F
11

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

Sample Tag class

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

}

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

Flagman answered 12/5, 2014 at 5:0 Comment(2)
Ok I'm going to try it, I'm sure I'm missing something, maybe a I need a break, thank youAarau
@Shereef thank you, I saw my mistake, I should to use setTag after this line " holder.code.setText(storeItem.getCode())".. using "holder.share.setTag(storeItem.getCode())", this is the generic answer thak you!! :DAarau
T
5

You are very close to your answer. Just Follow the changes and complete your answer

@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;
    }
Teeming answered 12/5, 2014 at 5:7 Comment(4)
@ Biraj Ok, so if I add this changes on my custom adapter, what is the code using getTag on my "public void shareOnClickHandler(View v)" to get for example the text from my textview? tyAarau
Not getting bro pleas be more precisely.Teeming
@ Biraj - Ok.. I have a listview which is using a custom adapter with 3 views : Textview, Webview and Button, so when I use SetAdapter firstime on Main Activity I load my listview with the views (webview, textview and button) that is ok..continueAarau
..but then I need to add 2 actions to my Button, 1 is get the text from textView (obviously from same view) because this text has an idText that contains the key to call another text saved on SharedPreference, exactly a path from an image, and (2 action) this image will be Load in the webView, changing the original (flipimage) and I need to do that in my principal activity (shareOnClickListener method) using getTag, that is the code I need, hope this helpAarau
G
1

Answer 1:

What you want to do it:

above or right after this line: holder.code.setText(storeItem.getCode()); add the following: holder.share.setTag(storeItem.getCode());

and in the 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
}

Or

Answer 2:

You may not need to use setTag and getTag if you do the following

  1. Go to your xml and remove the onclick attribute from the button we are going to use it in the Java instead
  2. Use the following getView

    @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.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;
    }
    
Germen answered 12/5, 2014 at 5:35 Comment(1)
OK I'm going to try it, I really appreciate your time, I can't test right now 'cause my webservice is down =(, when it is working I will test it and check it like the answer thank you!Aarau

© 2022 - 2024 — McMap. All rights reserved.