2013-10-10 21 views
5

Friends、Android ExpandableListView ChildViewのCheckBox

ChildViewで単一選択チェックボックスを使用するExpandableListViewを作成しようとしています。 そして、私はExpandableListViewのOnChildClickListener()で他のCheckBoxを "false"に設定する方法を理解できません。ここに私のコードは次のとおりです。ここ

ExpListView.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id) { 
       CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox); 
       if (cb.isChecked()) {   

       } else { 
        cb.setChecked(true); 
        //Here somehow I must set all other checkboxes to false. 
          //Is it possible? 
       } 
       return false; 
      } 
    }); 

はChildViewのxmlです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 

    <TextView 
    android:id="@+id/textChild" 
    android:layout_width="wrap_content" 
    android:layout_height="40dp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="20dp" 
    android:textColor="@android:color/white" 
    android:layout_weight="1" 
    /> 

<CheckBox android:id="@+id/checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:clickable="false" 
     android:layout_gravity="right" 
     android:visibility="visible" 
/> 

</LinearLayout> 
+0

ExpandableListViewのすべての子を再帰的に移動し、別のチェックボックスを見つけることができます。選択したセクションの親ビューを最初に見つけて(自分のIDがわからない場合)、そのチェックボックスを取得することをお勧めします。または、チェックボックスを配列で保持し、チェック状態を変更してnotifyDataSetChanged()メソッドを呼び出します。 –

+0

2番目の提案を教えてもらえますか?これは私がしたいことですが、私はAndroidで新しく、選択したセクションで他のチェックボックスを探す方法を理解できません – Dlash

+0

ExpListViewの初期化と読み込みに関連するコードをもっと提供できますか? –

答えて

5

あなたが唯一のチェックボックスを選択することができるようにしたい場合は、変数CheckBox checkedBox;で確認するチェックボックスを格納することができます。 CheckBoxをクリックすると、あなたは

@Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
       int groupPosition, int childPosition, long id) { 
      CheckBox last = checkedBox //Defined as a field in the adapter/fragment 
      CheckBox current = (CheckBox) v.findViewById(R.id.checkbox); 

      last.setCheked(false); //Unchecks previous, checks current 
      current.setChecked(true); // and swaps the variable, making 
      checkedBox = current;  // the recently clicked `checkedBox` 

      return false; 
     } 

けれどもの線に沿って何かをすることができ、アンドロイドは、リサイクルシステムを見ると、これが動作するかどうかはわからないんだけど、それはショットの価値があります。

複数の選択肢が必要な場合は、checkedBoxList<CheckBox>に展開して、チェックボックスをオフにする必要があるたびに繰り返します。

(おそらく必要な)いくつかの追加データを保存する必要がある場合は、ホルダークラスを作成することができます。

class CheckBoxHolder{ 

    private CheckBox checkBox: 
    private int id; 

    public CheckBoxHolder(CheckBox cb, int id){ 
     this.checkBox = cb; 
     this.id = id; 
    } 
    // Getter and/or setter, etc. 
} 
関連する問題