title: Android CheckedTextView - Changing checked status on click
body: <p>I have a listview that needs to be multiple choice (i.e each list item has a checkbox which can be checked / unchecked)
  The list view is in a tabhost and is the content for teh first tab.</p>
  
  <p>My set up is like so:</p>
  
  <p>My tab is set up with:</p>
  
  <pre><code>TabSpec tab = tabHost.newTabSpec("Services");
  
  tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Services").setContent(new Intent(this, ServiceList.class)));
  </code></pre>
  
  <p>When the tab is clicked new activity ServiceList is started</p>
  
  <p>ServiceList is defined as such:</p>
  
  <pre><code>    public class ServiceList extends ListActivity{
          private EscarApplication application;
          ListView listView;
           @Override
             public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.service_list);
               ServiceList.this.application = (EscarApplication) this.getApplication();
               final ListView listView = getListView();
           }
  
           protected void onListItemClick(ListView l, View v, int position, long id) {
              String.valueOf(id);
              Long.toString(id);
              ((CheckedTextView) v).setChecked(true);
              super.onListItemClick(l, v, position, id);
          }
  
           @Override
           public void onStart() {
               super.onStart();
              //Generate and display the List of visits for this day by calling the AsyncTask
               GenerateServiceList services = new GenerateServiceList();
               int id = ServiceList.this.application.getVisitId();
               services.execute(id);
               listView  = getListView();
               listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
           }
  
          //The AsyncTask to generate a list
           private class GenerateServiceList extends AsyncTask&lt;Integer, String, Cursor&gt; {
                // can use UI thread here
                protected void onPreExecute() {
                }
                // automatically done on worker thread (separate from UI thread)
                protected Cursor doInBackground(Integer...params) {
                    int client_id = params[0];
                    ServiceList.this.application.getServicesHelper().open();
                    Cursor cur = ServiceList.this.application.getServicesHelper().getPotentialVisitServices(client_id);
                    return cur; 
  
                }
                // can use UI thread here
                protected void onPostExecute(Cursor cur){  
                    startManagingCursor(cur);
                    // the desired columns to be bound
                    String[] columns = new String[] {ServicesAdapter.KEY_SERVICE};
                    // the XML defined views which the data will be bound to
                    int[] to = new int[] {R.id.display_service};
                    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(ServiceList.this, R.layout.service_list_element, cur, columns, to);
                    // set this adapter as your ListActivity's adapter
                    ServiceList.this.setListAdapter(mAdapter);
  ServiceList.this.application.getServicesHelper().close();       
  
                }
           }
      }
  </code></pre>
  
  <p>So, everything works ok until I click on my list item to change the checkbox state.</p>
  
  <p>The part of teh code set to handle click events is causing me problems:</p>
  
  <pre><code> protected void onListItemClick(ListView l, View v, int position, long id) {
                  String.valueOf(id);
                  Long.toString(id);
                  ((CheckedTextView) v).setChecked(true);
                  super.onListItemClick(l, v, position, id);
              }
  </code></pre>
  
  <p>My understanding is that the View v passed to the onListItemClick method reperesents my list element, so I am trying to cast v as a CheckedTextView and set teh checked value to true, however this just causes my app to crash. Am I missing something simple here, or is there an easier way to do this?</p>
  
  <p>Thanks</p>
  
  <p>Kevin</p>