2012-02-27 12 views
1

私は、BaseExpandableListAdapterに裏打ちされたExpandableListViewを持っています。子リスト項目はすべてCheckBoxです。ExpandableListViewグループを折りたたんでいると、自分のチェックボックスがオフになるのはなぜですか?

グループを展開し、チェックボックスをオンにしてグループを折りたたむ。このチェックボックスはオフになっています。

私のログ出力は次のようになります。

-- I check the checkbox: 
02-27 13:51:29.674: D/ExpandableListAdapter(3583): Option '2' CHECKED 
--- I collapse the group, which unchecks the checkbox: 
02-27 13:51:30.850: D/ExpandableListAdapter(3583): Option '2' UNCHECKED 

あなたが見ることができるように、グループを崩壊すると、そのグループ内のチェックボックスのすべての私のOnCheckedChangedListener(おそらく間接的に)呼んでいます。

私のコードは次のとおりです。

public class TestExpandableListActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ExpandableListView elv = 
      (ExpandableListView) findViewById(R.id.optionsELV); 
     ExpandableListAdapter ela = new ExpandableListAdapter(this); 
     elv.setAdapter(ela); 
    } 
    public class ExpandableListAdapter extends BaseExpandableListAdapter { 
     private static final String TAG = "ExpandableListAdapter"; 
     private final String[] groups; 
     private final String[][] children; 
     private final boolean[][] childrenChecked; 
     private final Context context; 
     public ExpandableListAdapter(Context c) { 
      this.context = c; 
      groups = new String[] { "Options A", "Options B" }; 
      children = 
       new String[][] {{"A", "B", "C"},{"1", "2", "3"}}; 
      childrenChecked = 
       new boolean[][] {{false, false, false},{false, false, false}}; 
     } 
     public View getChildView(
       final int groupPos, 
       final int childPos, 
       boolean isLastChild, 
       View convertView, 
       ViewGroup parent) { 
      CheckBox cb = new CheckBox(context); 
      cb.setText(children[groupPos][childPos]); 
      cb.setChecked(childrenChecked[groupPos][childPos]); 
      cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       public void onCheckedChanged(
         CompoundButton buttonView, 
         boolean isChecked) { 
        Log.d(TAG, 
          String.format("Option '%s' %s", 
            children[groupPos][childPos], 
            isChecked ? "CHECKED" : "UNCHECKED")); 
        childrenChecked[groupPos][childPos] = isChecked; 
       } 
      }); 
      return cb; 
     } 
     public int getGroupCount() { 
      return groups.length; 
     } 
     public int getChildrenCount(int groupPos) { 
      return children[groupPos].length; 
     } 
     public String getGroup(int groupPos) { 
      return groups[groupPos]; 
     } 
     public String getChild(int groupPos, int childPos) { 
      return children[groupPos][childPos]; 
     } 
     public long getGroupId(int groupPos) { 
      return (1024 * (groupPos + 1)); 
     } 
     public long getChildId(int groupPos, int childPos) { 
      return (1024 * (groupPos + 1)) + (childPos + 1); 
     } 
     public View getGroupView(
       int groupPos, 
       boolean isExpanded, 
       View convertView, 
       ViewGroup parent) { 
      TextView tv = new TextView(context); 
      tv.setText(groups[groupPos]); 
      return tv; 
     } 
     public boolean hasStableIds() { 
      return true; 
     } 
     public boolean isChildSelectable(int groupPos, int childPos) { 
      return true; 
     } 
    } 
} 

私のレイアウトは、(私が適切に多肢選択法にchoiceModeを設定していることに注意してください)です。

<?xml version="1.0" encoding="utf-8"?> 
<ExpandableListView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/optionsELV" 
    android:choiceMode="multipleChoice" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

はなぜ私のチェックボックスをすべてオフにグループを崩壊さ?これを防ぐ方法を教えてください。

答えて

1

解決策が見つかったと思います— CheckBoxの代わりにCheckedTextViewを使用します。

public View getChildView(
     final int groupPos, 
     final int childPos, 
     boolean isLastChild, 
     View convertView, 
     ViewGroup parent) { 
    final CheckedTextView ctv = new CheckedTextView(context); 
    ctv.setChecked(childrenChecked[groupPos][childPos]); 
    ctv.setCheckMarkDrawable(
      ctv.isChecked() 
      ? android.R.drawable.checkbox_on_background 
      : android.R.drawable.checkbox_off_background); 
    ctv.setText(children[groupPos][childPos]); 
    ctv.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      ctv.toggle(); 
      Log.d(TAG, 
       String.format("Option '%s' %s", 
        children[groupPos][childPos], 
        ctv.isChecked() ? "CHECKED" : "UNCHECKED")); 
      childrenChecked[groupPos][childPos] = ctv.isChecked(); 
      ctv.setCheckMarkDrawable(
        ctv.isChecked() 
        ? android.R.drawable.checkbox_on_background 
        : android.R.drawable.checkbox_off_background); 
     } 
    }); 
    return ctv; 
} 

CheckedTextViewのDOCOは—「それのsetChoiceModeがCHOICE_MODE_NONE以外に設定されているリストビューで使用する場合、これは便利です」CheckedTextViewは、リストビュー—で有用であることを言うんが、私はかなりよCheckBoxとCheckedTextViewの違いと、CheckBoxesが完全に機能しないように見える理由については不明です。

CheckBoxとCheckedTextViewの違いと、CheckBoxesがExpandableListView内で異常に機能する理由を教えていただけますか?

+0

でも確認されたテキストビューで迷惑なんだ、あなたはこのような問題に直面したのですか? – Harshit

0

私はこの同じ問題を抱えていました。なぜ、ExpandableListViewのセグメントを移動、拡大、折りたたむと、チェックボックスがオフになることがあるのか​​はわかりません。

もっと簡単な方法は、OnCheckedChangeの代わりにCheckBoxのOnClickListenerイベントを使用することです。

あなたが崩壊またはテキストビューを展開し、リストの残りのためにランダムに変更を切り替える場合は

関連する問題