不调用RadioButton OnCheckedChanged

时间:2013-04-23 20:09:14

标签: android expandablelistview radio-group

从我的MainViewExpandableListView)我得到RadioButton已定义child_row.xml,我设置了侦听器,但OnCheckChanged未被调用。如果我将RadioButton移动到currentview,它就会被调用。

我该如何解决这个问题?

我有......

  1. mainListView.xml // expandableListView
  2. group_row.xml // textView
  3. child_row.xml //具有自定义radioButton
  4.  public class Main_ListView extends Activity implements OnCheckedChangeListener{ 
     public void onCreate(Bundle savedInstanceState) {
     try{
             super.onCreate(savedInstanceState);
             setContentView(R.layout.mainListView);
    
          ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView1);
          SimpleExpandableListAdapter expListAdapter =
                    new SimpleExpandableListAdapter(
                            getApplicationContext(),
                            createGroupList(),              // Creating group List.
                            R.layout.group_row,             // Group item layout XML.
                            new String[] { "Group Item" },  // the key of group item.
                            new int[] { R.id.row_name },    // ID of each group item.-Data under the key goes into this TextView.
                            createChildList(),              // childData describes second-level entries.
                            R.layout.child_row,             // Layout for sub-level entries(second level).
                            new String[] {"Sub Item"},      // Keys in childData maps to display.
                            new int[] { R.id.grp_child}     // Data under the keys above go into these TextViews.
                        );
                    elv.setAdapter(expListAdapter);       // setting the adapter in the list.
    
            }catch(Exception e){
                   System.out.println("Errrr +++ " + e.getMessage());
            }
    
    
        final LayoutInflater factory = getLayoutInflater();
        final View childview = factory.inflate(R.layout.child_row, null);
    
        answersGroup = (SegmentedRadioGroup) childview.findViewById(R.id.answersGroup);
        if(answersGroup != null)
           answersGroup.setOnCheckedChangeListener(this);
    
    }
    @Override   
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (group == answersGroup) {
            if (checkedId == R.id.radio0) {
                mToast.setText("One");
                mToast.show();
            } else if (checkedId == R.id.radio1) {
                mToast.setText("Two");
                mToast.show();
            } else if (checkedId == R.id.radio2) {
                mToast.setText("Three");
                mToast.show();
            }
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

您没有提供您使用的完整代码。如果您稍后将那个膨胀的 childView 添加到Activity的布局中,那么您应该提供主要布局,看看您是否以某种方式阻止了CheckBox。如果你只是夸大了布局,那么你没有得到被检查的事件是正常的,因为 childView 不在屏幕上的任何位置。

从代码的外观来看,您似乎想要为CheckBox的子级中的ExpandableListView设置一个侦听器。如果这是你想要的,那么这是一次不正确的尝试,而这样做的方法是为ExpandableListView使用自定义适配器。这里有很多教程(和问题),用于构建自定义适配器并将侦听器设置为子元素。

相关问题