title: How to toggle change background of a list item in android
body: <p>What's wrong there? I have a listview and I want to change the background of an item on clicking list item.If I select an item and then click on different item then toggle the background to the another. This is the single selection process</p>
  
  <pre><code>if (convertView == null)  
  {  
  convertView = mInflater.inflate(R.layout.customer_account_customview, null);  
  holder = new ViewHolder();
  holder.tr = (TableRow)convertView.findViewById(R.id.tableRow);
  convertView.setTag(holder);
  }  
  else  
  {  
  holder = (ViewHolder) convertView.getTag();
  }
  holder.tr.setTag((Integer)position);
  
  holder.tr.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View view)
  {
      int tableRowPosition=(new Integer((Integer) view.getTag())).intValue();
      Log.i("Position of Row click", ""+tableRowPosition);
      holder.tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.abc));
  }
  });
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/AFFNd.png" alt="enter image description here"></p>
  
  <pre><code>class CustomerAccountAdapter extends BaseAdapter
       {
  
           public CustomerAccountAdapter() {
               mInflater = LayoutInflater.from(CustomerAccount.this);
               selectedIds = new ArrayList&lt;Integer&gt;();
          }
  
           @Override
              public int getCount() {
                  // TODO Auto-generated method stub
                  return arraylist_invoices.size();
              }
  
              @Override
              public Object getItem(int position) {
                  // TODO Auto-generated method stub
                  return arraylist_invoices.get(position);
              }
  
              @Override
              public long getItemId(int position) {
                  return position;
              }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              vvw = convertView;
  
              Log.i("inside", "getView()");
  
              if (vvw == null)  
              {  
              vvw = mInflater.inflate(R.layout.customer_account_customview, null);  
              holder = new ViewHolder();
  
              holder.tableRow = (TableRow)vvw.findViewById(R.id.tableRow10);
  
  
                  vvw.setTag(holder);
              }  
              else  
              {  
                  holder = (ViewHolder) vvw.getTag();
              }           
              holder.tableRow.setTag((Integer)position);
  
              holder.tableRow.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View view)
                  {
                      // TODO Auto-generated method stub
                      Integer myIntegerObject=new Integer((Integer) view.getTag());
                      tableRowPosition=myIntegerObject.intValue();
                      Log.i("Position of Row click", ""+tableRowPosition);
  
                  //  notifyDataSetChanged();
                  //  holder.tableRow.setBackgroundResource(R.drawable.table_heading_shape);
  
                  //  holder.tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.cancel_big_butt));
                      notifyDataSetChanged();
                  //  holder.tableRow.setBackgroundResource();
  
                  }
              });
  
                  return vvw;
  
          }
       }
          private class ViewHolder
      {  
          TextView txtDate, txtInv, txtAmt, txtPaid, txtBal,txtDays;
          TableRow tableRow;// All TextView inside TableRow
      }
  </code></pre>