title: Can't get actual Checked Items from ListView
body: <p>I'm having a problem getting the Checked Items on my listview.
  The thing is that No matter what when i call the getCheckedItemsCount() o getCheckedItemPositions() it always returns 1. No matter if there's 0 or 2 or more items checked.</p>
  
  <p>This is my MainActivity, which implements MultiChoiceModeListener, to listen for when an item is checked. 
  I do this because i check the items dynamically on the ListAdapter.</p>
  
  <pre><code>public class MainActivity extends ListActivity implements MultiChoiceModeListener
  {
      @Override
      protected void onCreate (Bundle bundle)
      {
          super.onCreate (bundle);
          // Set our view from the "main" layout resource
          setContentView (R.layout.main);
  
          this.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
          this.getListView().setMultiChoiceModeListener(this);
  
          _dataAdapter = new ServerListAdapter (this);
          this.getListView(). setAdapter(_dataAdapter);
          registerForContextMenu (this.getListView());
      }
  
      // This is called when i set the item as checked using setItemChecked on my ListAdapter.
      public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean isChecked) {
  
  
          SparseBooleanArray checke = getListView().getCheckedItemPositions();
          // This always returns 1. No matter If I have 0 or 2 items checked.
          int checkedCount = checkedItemPositions.size();
  
          // I Have also tried with getCheckedItemsCount() and it returns 1 too.
  
          if (checkedCount &gt; 0)
          {
              // DO SOMETHING...
          }
          else
          {
                  // DO SOME OTHER STUFF...
          }
      }
  </code></pre>
  
  <p>And here is the code for my ListAdapter. Only relevant code is here:</p>
  
  <pre><code>public class ServerListAdapter extends BaseAdapter {
      @Override
      public View getView (final int position, final View convertView, final ViewGroup parent)
      {
          final ListView listView = (ListView)parent;
          boolean isChecked = listView.isItemChecked(position);
  
          ((CheckBox)view.findViewById(R.id.chkItemServerSelected)).setOnCheckedChangeListener(new OnCheckedChangeListener() 
          {   
  
  
              @Override
              public void onCheckedChanged(CompoundButton pCompound, boolean arg1) 
              {
  
                  boolean isChecked = listView.isItemChecked(position);
                  // Here i set the item as checked, o unchecked. This works ok.
                  listView.setItemChecked(position, !isChecked);
              }
  
          });
  
          //Finally return the view
          return view;
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After looking around, i found out that the problem was that I was doing it the wrong way. </p>
  
  <p>On my list adapter, on the onCheckedChanged, instead of using the current value get from the list view, I had to use the value from the checkbox, (since that's what I'm trying to achieve).</p>
  
  <p>Previous code:</p>
  
  <pre><code>listView.setItemChecked(position, !isChecked);
  </code></pre>
  
  <p>New code:</p>
  
  <pre><code>listView.setItemChecked(position, pCompound.isChecked());
  </code></pre>
  
  <p>The thing is that this brought a new problem.
  When the checked value IsChecked is <strong>TRUE</strong>, the event onItemCheckedStateChanged is raised, but when the value is <strong>FALSE</strong> it doesn't... any clues?</p>