title: When scrolling custom ListView, the checkbox value changes
body: <p><br>
  <strong>What I have:</strong> a custom listview with Textviews and checkbox.<hr/>
  <strong>Problem:</strong> Suppose my screen can show only 6 items of list at time, and the others are invisible.
  So I checked the first element on the list (item at position 0). I scroll down to see all my list, when I scroll up at first element (item at position 0) the checkbox is checked correctly. Great! But now there are new items thath are checked too, for example the item at position 8 (because when I scroll the list (for theRycicling) it become the new item at position 0..remember my screen shows only 7 elements at time). And it become checked, but i have never checked it!<br><hr/>
  <strong>Question</strong>: How i can avoid this problem? I don't want that the checkbox that I don't click change their status.
  <hr/>
  Below my adapter for listView with the getView function where I have implemented the setOnCheckedChangeListener:</p>
  
  <pre><code>public class NewQAAdapterSelectFriends extends BaseAdapter {
  private LayoutInflater mInflater;
  private Person[] data;
  
  
  public NewQAAdapterSelectFriends(Context context) { 
      mInflater = LayoutInflater.from(context);
  }
  
  public void setData(Person[] data) {
      this.data = data;
  }
  
  @Override
  public int getCount() {
      return data.length;
  }
  
  @Override
  public Object getItem(int item) {
      return data[item];
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
          convertView = mInflater.inflate(R.layout.item_select_friends, null);
          final ViewHolder viewHolder = new ViewHolder();
          viewHolder.nameText=(TextView) convertView.findViewById(R.id.personName);
          viewHolder.surnameText=(TextView) convertView.findViewById(R.id.personSurname);
          viewHolder.contactImage=(ImageView) convertView.findViewById(R.id.personImage);
          viewHolder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
          convertView.setTag(viewHolder);
          viewHolder.nameText.setTag(viewHolder.nameText);
          viewHolder.nameText.setTag(viewHolder.surnameText);
          viewHolder.contactImage.setTag(data[position]);
          viewHolder.checkBox.setOnCheckedChangeListener(
                  new CompoundButton.OnCheckedChangeListener() {
                      @Override
                      public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                          Person element = (Person) viewHolder.checkBox.getTag();
  
                          if(isChecked){
                              element.setCheck(buttonView.isChecked());
                              //data[position].setCheck(true); //this is equivalent to previous line
  
                          }
                          else{
                              //to-do
  
                          }
  
                      }
                  });
          viewHolder.checkBox.setTag(data[position]);
  
      } else {
  
  
      }
  
       ViewHolder holder = (ViewHolder) convertView.getTag();
       holder.nameText.setText(data[position].getName());
       holder.surnameText.setText(data[position].getSurname());
       holder.contactImage.setImageResource(data[position].getPhotoRes());
       holder.contactImage.setScaleType(ScaleType.FIT_XY);
       holder.checkBox.setChecked(data[position].isCheck());
      return convertView;
  }
  
  static class ViewHolder {
      TextView nameText;
      TextView surnameText;
      ImageView contactImage;
      CheckBox checkBox;
  }
  }
  </code></pre>
  
  <hr/>
  
  <p>Thanks for the answers :) </p>
  
  <p><strong>EDIT: With the suggestion i changed my getView like below:</strong></p>
  
  <pre><code>@Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      final ViewHolder viewHolder;
      if (convertView == null) {
          convertView = mInflater.inflate(R.layout.item_select_friends, null);
          viewHolder=new ViewHolder();
          viewHolder.nameText=(TextView) convertView.findViewById(R.id.personName);
          viewHolder.surnameText=(TextView) convertView.findViewById(R.id.personSurname);
          viewHolder.contactImage=(ImageView) convertView.findViewById(R.id.personImage);
          viewHolder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
  
          convertView.setTag(viewHolder);
  
          viewHolder.nameText.setTag(viewHolder.nameText);
          viewHolder.nameText.setTag(viewHolder.surnameText);
          viewHolder.contactImage.setTag(data[position]);
          viewHolder.checkBox.setChecked(data[position].isCheck());  
          viewHolder.checkBox.setOnClickListener(new OnClickListener() {  
  
              public void onClick(View arg0) {  
  
                  if(viewHolder.checkBox.isChecked()==true)   
                      data[position].setCheck(true);  
                  else  
                      data[position].setCheck(false);  
              }  
          }); 
  
      }
      else{
          viewHolder = (ViewHolder) convertView.getTag();
      }
  
      viewHolder.nameText.setText(data[position].getName());
      viewHolder.surnameText.setText(data[position].getSurname());
      viewHolder.contactImage.setImageResource(data[position].getPhotoRes());
      viewHolder.contactImage.setScaleType(ScaleType.FIT_XY);
      viewHolder.checkBox.setChecked(data[position].isCheck());
      return convertView;
  }
  </code></pre>
  
  <p><strong>SITUATION AFTER EDIT: Now, if i check the initial items that are shows on the screen and then I scrool the list, their value is correctly saved. But When I scroll the list and for example i want to check the last tree checkboxs of my list then after I scroll they become uncheked....BUT WHY??????</strong></p>
  
  <p><strong>SOLVED</strong>: I solved my problem of getView with this code:</p>
  
  <pre><code>    public class NewQAAdapterSelectFriends extends BaseAdapter {
  private LayoutInflater mInflater;
  private Person[] data;
  boolean[] checkBoxState;
  ViewHolder viewHolder;
  
  public NewQAAdapterSelectFriends(Context context) { 
      mInflater = LayoutInflater.from(context);
  }
  
  
  public void setData(Person[] data) {
      this.data = data;
      checkBoxState=new boolean[data.length];
  }
  
  @Override
  public int getCount() {
      return data.length;
  }
  
  @Override
  public Object getItem(int item) {
      return data[item];
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
          convertView = mInflater.inflate(R.layout.item_select_friends, null);
          viewHolder=new ViewHolder();
  
          viewHolder.nameText=(TextView) convertView.findViewById(R.id.personName);
          viewHolder.surnameText=(TextView) convertView.findViewById(R.id.personSurname);
          viewHolder.contactImage=(ImageView) convertView.findViewById(R.id.personImage);
          viewHolder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
  
          convertView.setTag(viewHolder);
  
      }
      else{
          viewHolder = (ViewHolder) convertView.getTag();
      }
  
      viewHolder.nameText.setText(data[position].getName());
      viewHolder.surnameText.setText(data[position].getSurname());
      viewHolder.contactImage.setImageResource(data[position].getPhotoRes());
      viewHolder.contactImage.setScaleType(ScaleType.FIT_XY);
      viewHolder.checkBox.setChecked(checkBoxState[position]);
      viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
  
             public void onClick(View v) {
                 if(((CheckBox)v).isChecked()){
                     checkBoxState[position]=true;
                     data[position].setCheck(true);
                 }else{
                     checkBoxState[position]=false;
                     data[position].setCheck(false);
                 }
              }
          });
      return convertView;
  }
  
  
  static class ViewHolder {
      TextView nameText;
      TextView surnameText;
      ImageView contactImage;
      CheckBox checkBox;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have seen this tutorial to do my getView: <a href="http://androidcocktail.blogspot.it/2012/04/adding-checkboxes-to-custom-listview-in.html" rel="noreferrer">http://androidcocktail.blogspot.it/2012/04/adding-checkboxes-to-custom-listview-in.html</a></p>