How to use setTag and getTag with custom adapter

前端 未结 3 1323
囚心锁ツ
囚心锁ツ 2020-12-30 06:04

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
相关标签:
3条回答
  • 2020-12-30 06:19

    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;
      }
      
    0 讨论(0)
  • 2020-12-30 06:28

    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

    0 讨论(0)
  • 2020-12-30 06:32

    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;
        }
    
    0 讨论(0)
提交回复
热议问题