title: What is the working of setTag and getTag in ViewHolder pattern?
body: <p>I have a simple code snippet for implementing custom listview.</p>
  
  <p>My code is as follows:</p>
  
  <p><strong>WeatherAdapter.java :</strong></p>
  
  <pre><code>public class WeatherAdapter extends ArrayAdapter&lt;weather&gt;{
  
      Context mcontext; 
      int mlayoutResourceId;    
     weather mdata[] = null;
     View row;
  
      public WeatherAdapter(Context context, int layoutResourceId, weather[] data) {
          super(context, layoutResourceId, data);
          mlayoutResourceId = layoutResourceId;
         mcontext = context;
          mdata = data;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        row = convertView;
          WeatherHolder holder = null;
  
          if(row == null)
          {
              LayoutInflater inflater = ( (Activity) mcontext).getLayoutInflater();
              row = inflater.inflate(mlayoutResourceId, parent, false);
  
              holder = new WeatherHolder(row);
  
  
              row.setTag(holder);
  
          }
          else
          {
              holder = (WeatherHolder)row.getTag();
          }
  
          weather w = mdata[position];
          holder.txtTitle.setText(w.mtitle);
          holder.imgIcon.setImageResource(w.micon);
  
          return row;
      }
  </code></pre>
  
  <p><strong>WeatherHolder.java</strong>:</p>
  
  <pre><code>class WeatherHolder
      {
          ImageView imgIcon;
          TextView txtTitle;
  
  
      public WeatherHolder(View v){
  
            imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            txtTitle = (TextView)row.findViewById(R.id.txtTitle);
  
      }
      }
  }
  </code></pre>
  
  <p>I have seen so many answers on SO and other sites and I understood the recycling mechanism of listview.</p>
  
  <p>I also understood that from viewholder, we can hold the child views in the adapter and we do not have to call <code>findViewById()</code> many times. So, it is for optimization.</p>
  
  <p>But I have only the confusion in <code>setTag(holder)</code> and <code>getTag()</code> methods. <a href="https://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view">From this question</a>, I came to know that it is for making a key-value pair on multiple objects, so that we can access them easily. But, I do not understand why they are required here...because, we do not have multiple holder objects...only we have to change holder's variables each time. can we code here without using <code>setTag</code> and <code>getTag</code>?</p>
  
  <p>can anyone explain better that what <code>setTag</code> and <code>getTag</code> do "here"? </p>