2010-02-28 6 views
0
CheckBox

J'ai un TabHost qui contient 5 TabHost.TabSpec. Chaque TabSpec est un ListView qui est rempli à l'aide de SimpleCursorAdapter, la source de données étant une base de données sqlite3.Android: TabHost avec ListView de

La disposition utilisée par SimpleCursorAdapter contient 2 TextViews qui contiennent des données de base de données (une cachée - qui contient l'enregistrement de la base de données _id, et une affichée). Le troisième widget est un CheckBox. Voir le schéma ci-dessous:

<RelativeLayout 
    android:id="@+id/favoriteRow" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
<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> 

Mon principal problème est que je ne peux pas comprendre comment capturer/écouter lorsque l'utilisateur « clique » sur la case correspondante. J'ai sous-classé CheckBox avec FavoriteCheckBox et ajouté un protected void onClick(View v), mais je n'arrive jamais là quand je clique sur une case à cocher.

Toute suggestion sur ce qui me manque.

TIA,

jb

Répondre

0

Vous auriez tout simplement d'ajouter un écouteur à l'instance que vous créez dans votre 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/