views:

358

answers:

1

I have a TabHost that contains 5 TabHost.TabSpec. Each TabSpec is a ListView that is populated using SimpleCursorAdapter, with the datasource being an sqlite3 database.

The layout used by SimpleCursorAdapter contains 2 TextViews which hold database data (one hidden - which contains the database record _id, and one displayed). The 3rd widget is a CheckBox. See layout below:

<RelativeLayout 
  android:id="@+id/favoriteRow" 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  xmlns:android="http://schemas.android.com/apk/res/android"&gt;
<TextView 
  android:id="@+id/text0" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:visibility="gone"
  android:paddingLeft="5px">  
</TextView>
<TextView 
  android:id="@+id/text1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@+id/text0"
  android:textColor="@color/listTextColor"
  android:textSize="@dimen/font_size_for_show_row"
  android:paddingTop="@dimen/vertical_padding_for_show_row"
  android:paddingBottom="@dimen/vertical_padding_for_show_row">
</TextView>  
<com.example.subclass.FavoriteCheckBox 
  android:text=""
  android:id="@+id/favorite_checkbox"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:checked="false">
</com.example.subclass.FavoriteCheckBox>
</RelativeLayout>

My main problem is I can't figure out how to capture/listen when the user 'clicks' on the CheckBox. I subclassed CheckBox with FavoriteCheckBox and added a 'protected void onClick(View v)', but I never get there when I click on a checkbox.

Any suggestion on what I'm missing.

TIA,

jb

A: 

You would simply have to add a listener to the instance you create in your code:

CheckBox repeatChkBx =
    ( CheckBox ) findViewById( R.id.favorite_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
        }

    }
});

Via: http://mgmblog.com/2008/02/18/android-checkbox-oncheckedchangelistener/

Ryan