0

从我的MainView( ExpandableListView) 我得到RadioButtondefined child_row.xml,我设置了监听器,但OnCheckChanged没有被调用。如果我移动RadioButton到 currentview,它会被调用。

我怎样才能解决这个问题?

我有 ....

  1. mainListView.xml //可扩展列表视图
  2. group_row.xml //文本视图
  3. child_row.xml //有自定义radioButton

 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();
        }
    }

}
4

1 回答 1

0

您没有提供您使用的完整代码。如果您稍后将该膨胀的childView添加到Activity的布局中,那么您应该提供主布局以查看您是否没有以某种方式阻止CheckBox. 如果您只是膨胀布局,那么您不会得到选中的事件是正常的,因为childView在屏幕上没有可以操作的位置。

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

于 2013-04-24T06:11:33.433 回答