我的应用程序会跟踪餐厅服务生的轮班销售额,以帮助他们进行预算。在显示过去班次的活动中,我在ListView下创建了一个RadioGroup,以便用户可以选择显示午餐、晚餐或两者都显示。
我已经在activity RadioGroup.onCheckedChangeListener中实现了,但是onCheckedChanged从未被调用过。我还尝试使用匿名内部类作为listener,同样的结果。
我尝试从以下答案复制/修改代码:https://stackoverflow.com/a/9595528 ...but当我将@Override添加到回调函数时,Eclipse编译器给我一个错误(而不是警告),该方法必须覆盖超类,而快速修复方法是删除覆盖。
我非常确定签名是匹配的,因为它们是使用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>有什么想法吗?
发布于 2012-10-22 02:19:28
发现了问题,但从我在问题中给出的信息看不到答案。问题就出在这里。
这是从我的第一个项目开始的,几个月前我笨手笨脚地试图解决一个数据库问题,我在onCreate()中用几乎相同的代码重写了onStart()和onRestart()。随着时间的推移,onCreate已经改变了,而这些方法并没有改变。一旦我删除了它们,代码就完美地工作了。
我在问题中没有展示这些方法。抱歉的!
感谢大家的帮助!我现在该怎么做?是否删除该问题?
发布于 2012-10-19 12:47:05
您已经将checkedId作为参数获取,这是新选中的单选按钮的惟一标识符。
public void onCheckedChanged(RadioGroup group, int checkedId) {
rbLunchOnly.setText("Click!");
Toast.makeText(getApplicationContext(), "Lunch Only", Toast.LENGTH_LONG).show();
switch(checkedId){
case R.id.RadioBoth :
populateAllShifts();
break;
//--other cases---
}发布于 2012-10-19 11:29:48
您应该使用此方法:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked == true)
{
if(buttonView == radioA)
tvInfo.setText("A");
else if(buttonView == radioB)
tvInfo.setText("B");
else if(buttonView == radioC)
tvInfo.setText("C");
}
}https://stackoverflow.com/questions/12966907
复制相似问题