我的应用程序跟踪餐厅服务员的班次销售情况,以帮助他们预算。在显示过去班次的活动中,我在ListView下创建了一个RadioGroup,以便用户可以选择显示午餐、晚餐或两者都显示。
我在该活动中实现了RadioGroup.onCheckedChangeListener,但onCheckedChanged从未被调用。我还尝试使用匿名内部类作为监听器,结果相同。
我试图复制/修改来自此答案的代码:https://stackoverflow.com/a/9595528 ,但当我在回调函数中添加
我很确定签名是匹配的,因为它们是使用Eclipse的自动完成和实现方法工具创建的。然后,我按照说明将我的java编译器从1.5移动到1.6,以上列出的任何行为似乎都没有改变。
以下是我认为与此相关的代码:
有什么想法吗?
我在该活动中实现了RadioGroup.onCheckedChangeListener,但onCheckedChanged从未被调用。我还尝试使用匿名内部类作为监听器,结果相同。
我试图复制/修改来自此答案的代码:https://stackoverflow.com/a/9595528 ,但当我在回调函数中添加
@Override
时,Eclipse编译器给出了一个错误(不是警告),提示该方法必须覆盖超类,并且快速修复是删除override。我很确定签名是匹配的,因为它们是使用Eclipse的自动完成和实现方法工具创建的。然后,我按照说明将我的java编译器从1.5移动到1.6,以上列出的任何行为似乎都没有改变。
以下是我认为与此相关的代码:
public class DataActivity extends ListActivity implements OnCheckedChangeListener{
RadioButton rbBoth;
RadioButton rbDinnerOnly;
RadioButton rbLunchOnly;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
// ...
final RadioGroup rgGroup = (RadioGroup)findViewById(R.id.DataRadioGroup);
rbBoth = (RadioButton)findViewById(R.id.RadioBoth);
rbDinnerOnly = (RadioButton)findViewById(R.id.RadioDinnerOnly);
rbLunchOnly = (RadioButton)findViewById(R.id.RadioLunchOnly);
rgGroup.setOnCheckedChangeListener(this);
populateAllShifts();
}
// ...
public void onCheckedChanged(RadioGroup group, int checkedId) {
rbLunchOnly.setText("Click!");
Toast.makeText(getApplicationContext(), "Lunch Only", Toast.LENGTH_LONG).show();
if(group.getCheckedRadioButtonId() == R.id.RadioBoth){
populateAllShifts();
return;
}
if(group.getCheckedRadioButtonId() == R.id.RadioLunchOnly){
populatLunchShifts();
return;
}
if(group.getCheckedRadioButtonId() == R.id.RadioDinnerOnly){
populateDinnerShifts();
return;
}
}
// ...
}
这个类中有一个带有自定义适配器的 ListView,但如果我的理解和我的 XML 正确的话,RadioGroup 应该在列表外:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llDataLayout"
android:weightSum="5"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<ListView android:layout_weight="4"
android:layout_width="fill_parent"
android:id="@android:id/list"
android:layout_height="wrap_content">
</ListView>
<RadioGroup
android:layout_weight="1"
android:id="@+id/DataRadioGroup"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RadioButton android:text="Lunch and Dinner"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioBoth"/>
<RadioButton android:text="Dinner Only"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioDinnerOnly"/>
<RadioButton android:text="Lunch Only"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioLunchOnly"/>
</RadioGroup>
</LinearLayout>
有什么想法吗?