title: Android listview with checkbox problem
body: <p>I have a weird problem! I'm trying to create a listview with checkboxes. In my other <a href="https://stackoverflow.com/questions/5438375/custom-listview-with-checkbox-problem">thread</a> I was told that I should use an array that keeps track of the rows that are checked. I did that and it worked fine-ish but the logic is wrong and I run into another problem now.</p>
  
  <pre><code>public View getView(final int position, View convertView, ViewGroup parent) {
          View v = convertView;
          if (v == null) {
              LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              v = vi.inflate(R.layout.row, null);
          }
  
          CheckBox checkbox = (CheckBox)v.findViewById(R.id.checkbox);
          checkbox.setChecked(checked[position]);
  
          final LinearLayout rowLayout = (LinearLayout) v.findViewById(R.id.individualRow);
  
          checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if(isChecked)
                  {
                      rowLayout.setBackgroundColor(Color.GRAY);                       
                      checked[position] = false;                      
                  }
                  else
                  {   
                      rowLayout.setBackgroundColor(Color.DKGRAY);
                      checked[position] = true;                       
                  }
              }               
          });
     }
  </code></pre>
  
  <p>Having all the checkboxes unchecked initially it works fine it keeps the ones that i select checked even if I scroll down and back up again but the checked array is not properly set up. Basically the if test should be the other way arround!</p>
  
  <pre><code>if(isChecked)
  {
     rowLayout.setBackgroundColor(Color.GRAY);                        
     checked[position] = true;                        
  }
  else
  {   
     rowLayout.setBackgroundColor(Color.DKGRAY);
     checked[position] = false;                       
  }
  </code></pre>
  
  <p>The problem is with the scroll really because every time I scroll the onCheckedChanged method is called and since its recycling the rows it passes in the position of the new row that its not selected but since it has the same index as the one that was previously selected it changes its value. for example if I check the box with index 2 (set it to true) and then scroll down a new row becomes row with index 2, the method is called again and it unsets the checkbox(the field in the checked array).</p>
  
  <p>I need it to "remember" all the boxes that are checked. In other words I want the checked array to be initialised properly. And also to remember which boxes are checked and not lose them everytime I scroll!</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Can you please help me?</p>
  
  <p>Thanks in advance -- Mike</p>