title: Android ListView Checkbox Selection
body: <p>I kind of have a 2 part question here.</p>
  
  <p>1) How can I populate my ListView so that strings are what is displayed, but when items are selected, a non-visible id value (contact id from phone contacts) is the value that is actually used?</p>
  
  <p>2) I have a ListView that's using multipleChoice mode for item selections. It's popluated with names from my contacts list. When I select an item in the ListView, I want that selected item to trigger a call to my SqLite routine to store the value into a database record. How do I make this event fire when an item is checked in the listview?</p>
  
  <p>This is my layout XML;</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
  &lt;ListView
      android:id="@+id/lvContacts"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:choiceMode="multipleChoice"
  /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And here is the code I am using to populate my ListView;</p>
  
  <pre><code>    private void fillData() {
      final ArrayList&lt;String&gt; contacts = new ArrayList&lt;String&gt;();
  
      // Let's set our local variable to a reference to our listview control
      // in the view.
      lvContacts = (ListView) findViewById(R.id.lvContacts);
  
      String[] proj_2 = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
      cursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
      while(cursor.moveToNext()) {
          // Only add contacts that have mobile number entries
          if ( cursor.getInt(2) == Phone.TYPE_MOBILE ) {
              String name = cursor.getString(1);
              contacts.add(name);
          }
      }
  
      // Make the array adapter for the listview.
      final ArrayAdapter&lt;String&gt; aa;
      aa = new ArrayAdapter&lt;String&gt;(this,
                                    android.R.layout.simple_list_item_multiple_choice,
                                    contacts);
  
      // Let's sort our resulting data alphabetically.
      aa.sort(new Comparator&lt;String&gt;() {
          public int compare(String object1, String object2) {
              return object1.compareTo(object2);
          };
      });
  
      // Give the list of contacts over to the list view now.
      lvContacts.setAdapter(aa);
  }
  </code></pre>
  
  <p>I had hoped to be able to use something like the onClick event for each checked item, but have made not one bit of progress.</p>
  
  <p>Any help would be so very appreciated. Thanks.</p>