title: Android - Listview with viewholder changing background of selected row on press
body: <p>I've been looking up posts all over this site and others, but ave not found working code for what I need. I currently have a listview with custom rows, and I have them alternate by color (grey and transparent). I am trying to get the selected row to become blue (and to go back to original color when another row is pressed), and stay selected even when I scroll away from it. </p>
  
  <p>EDIT: here is the complete list adapter, minus how a few icons are populated:</p>
  
  <pre><code>public class MultiActivateAdapter  extends ArrayAdapter&lt;Model&gt; {
  
  private final List&lt;Model&gt; list;
  private final List&lt;Model&gt; listInfo;
  private final List&lt;Model&gt; listAlert;
  private final List&lt;Model&gt; listSecondAlert;
  
  private int _index = -1;
  
  ArrayList&lt;String&gt; selectedStrings = new ArrayList&lt;String&gt;();
  
  private final Activity context;
  boolean checkAll_flag = false;
  boolean checkItem_flag = false;
  
  public MultiRenewAdapter2(Activity context, List&lt;Model&gt; list, List&lt;Model&gt; listInfo, List&lt;Model&gt; listAlert, List&lt;Model&gt; listSecondAlert) {
      super(context, R.layout.multirenewrow2, list);
      this.context = context;
      this.list = list;
      this.listInfo = listInfo;
      this.listAlert = listAlert;
      this.listSecondAlert = listSecondAlert;
  }
  
  static class ViewHolder {
      protected TextView text;
      protected ImageView alert;
      protected CheckBox checkbox;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
      LayoutInflater inflator = context.getLayoutInflater();
  
      ViewHolder viewHolder;
  
      if (convertView == null) {
          convertView = inflator.inflate(R.layout.multiactivaterow, null);
  
          viewHolder = new ViewHolder();
          viewHolder.alert = (ImageView) convertView.findViewById(R.id.alert);
          viewHolder.text = (TextView) convertView.findViewById(R.id.label);
          viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
  
          convertView.setTag(viewHolder);
          convertView.setTag(R.id.label, viewHolder.text);
          convertView.setTag(R.id.check, viewHolder.checkbox);
  
      } else {
          viewHolder = (ViewHolder) convertView.getTag();
      }
          viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                  list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
  
              }
          });
  
  
      if (position == _index)
      {
          convertView.setBackgroundColor(context.getResources().getColor(R.color.DeepSkyBlue));
      }
      else
      {
          convertView.setBackgroundColor(context.getResources().getColor(R.color.transparent));
          if (position % 2 == 0)
          {
              convertView.setBackgroundColor(Color.TRANSPARENT);
          }
          else
          {
              convertView.setBackgroundColor(Color.LTGRAY);
          }
      }
  
  
          viewHolder.checkbox.setTag(position); // This line is important.
  
      //some code removed to show icon population
  
      viewHolder.text.setText(list.get(position).getName());
      viewHolder.checkbox.setChecked(list.get(position).isSelected());
  
      return convertView;
  }
  
  public void setClicked(int index)
  {
      _index = index;
      this.notifyDataSetChanged();
  }
  }
  </code></pre>
  
  <p>And the code from the older activity to which the adapter used to belong:</p>
  
  <pre><code>listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
      {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, final View view, int position, long id) {
  
                  try {
                      adapt.setClicked(position);
  
                  } catch (Exception e)  {
                      System.out.println(e);
                  }
                  adapt.notifyDataSetChanged();
  ....
  </code></pre>
  
  <p>This code used to work for all of my other listViews, but I had to change things up due to it using/relying on different arrays and other things, that allowed me to save the selected checkboxes that are displayed.</p>