Java 单击时显示组失败

Java 单击时显示组失败,java,android,android-radiobutton,android-radiogroup,Java,Android,Android Radiobutton,Android Radiogroup,我动态地建立了一个无线电组,如下所示: void displayOpts(){ List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts(); //deleted line //next line changed radioGroup.setOrientation(LinearLayout.VERTICAL); for (UserAcco

我动态地建立了一个无线电组,如下所示:

void displayOpts(){
    List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts();
    //deleted line
    //next line changed
    radioGroup.setOrientation(LinearLayout.VERTICAL);
    for (UserAccountDetailsModel user : accounts){
        CompanyLocationsModel company = companyLocationSqliteModel.getCompany(user.getCompany_id());
        RadioButton rdbtn = new RadioButton(this);
        rdbtn.setTextSize(17);
        rdbtn.setId(company.getId());
        rdbtn.setText(user.getFirst_name() + " "+user.getLast_name() + " ---- " + company.getName());
        //next line changed
        radioGroup.addView(rdbtn);
    }
    //deleted line
    //next line changed
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
           // checkedId is the RadioButton selected
            Log.i("test", "Checked is "+checkedId);
        }
    });
}
在我的XML中,我有:

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:id="@+id/user_accounts_radios"
       android:layout_gravity="left"
       android:layout_marginLeft="20dp"
       android:layout_height="wrap_content"
       android:orientation="vertical">
关于设置用户

void displayOpts(){
    List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts();

    RadioGroup account_radios = new RadioGroup(this);
    account_radios.setOrientation(LinearLayout.VERTICAL);
    for (UserAccountDetailsModel user : accounts){
        CompanyLocationsModel company = companyLocationSqliteModel.getCompany(user.getCompany_id());
        RadioButton rdbtn = new RadioButton(this);
        rdbtn.setTextSize(17);
        rdbtn.setId(company.getId());
        rdbtn.setText(user.getFirst_name() + " "+user.getLast_name() + " ---- " + company.getName());
        account_radios.addView(rdbtn);
    }
    radioGroup.addView(account_radios);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
           // checkedId is the RadioButton selected
            Log.i("test", "Checked is "+checkedId);
        }
    });
}
void displayOpts(){
List accounts=userAccountDetailsSqliteModel.getAccounts();
放射组帐户\无线电=新放射组(此);
帐户\无线电设置方向(线性布局。垂直);
for(UserAccountDetailsModel用户:帐户){
CompanyLocationsModel company=companyLocationSqliteModel.getCompany(user.getCompany_id());
RadioButton rdbtn=新的RadioButton(本);
rdbtn.setTextSize(17);
setId(company.getId());
rdbtn.setText(user.getFirst\u name()+“”+user.getLast\u name()+“----”+company.getName());
帐户\无线电添加视图(rdbtn);
}
radioGroup.addView(帐户\无线电);
radioGroup.setOnCheckedChangeListener(新的radioGroup.OnCheckedChangeListener()
{
检查更改后的公共无效(RadioGroup组,int checkedId){
//checkedId是选中的单选按钮
Log.i(“测试”、“已检查为”+已检查EDID);
}
});
}
上面显示了
RadioGroup
,但我有以下问题:

  • 每当我点击第二个
    单选按钮时,第一个按钮将无法点击
    再次

  • 所选侦听器上的日志未记录


  • 我不知道出了什么问题,因为
    单选按钮显示正确。

    您正在创建一个新的
    放射组,并将其放入现有组中。这是错误的。只需像这样使用现有的一个:

    void displayOpts(){
        List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts();
        //deleted line
        //next line changed
        radioGroup.setOrientation(LinearLayout.VERTICAL);
        for (UserAccountDetailsModel user : accounts){
            CompanyLocationsModel company = companyLocationSqliteModel.getCompany(user.getCompany_id());
            RadioButton rdbtn = new RadioButton(this);
            rdbtn.setTextSize(17);
            rdbtn.setId(company.getId());
            rdbtn.setText(user.getFirst_name() + " "+user.getLast_name() + " ---- " + company.getName());
            //next line changed
            radioGroup.addView(rdbtn);
        }
        //deleted line
        //next line changed
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
               // checkedId is the RadioButton selected
                Log.i("test", "Checked is "+checkedId);
            }
        });
    }
    
    void displayOpts(){
    List accounts=userAccountDetailsSqliteModel.getAccounts();
    //删除行
    //下一行已更改
    射线组设置方向(线性布局。垂直);
    for(UserAccountDetailsModel用户:帐户){
    CompanyLocationsModel company=companyLocationSqliteModel.getCompany(user.getCompany_id());
    RadioButton rdbtn=新的RadioButton(本);
    rdbtn.setTextSize(17);
    setId(company.getId());
    rdbtn.setText(user.getFirst\u name()+“”+user.getLast\u name()+“----”+company.getName());
    //下一行已更改
    radioGroup.addView(rdbtn);
    }
    //删除行
    //下一行已更改
    radioGroup.setOnCheckedChangeListener(新的radioGroup.OnCheckedChangeListener()
    {
    检查更改后的公共无效(RadioGroup组,int checkedId){
    //checkedId是选中的单选按钮
    Log.i(“测试”、“已检查为”+已检查EDID);
    }
    });
    }
    
    您正在创建一个新的
    RadioGroup
    ,并将其放入现有组中。这是错误的。只需像这样使用现有的一个:

    void displayOpts(){
        List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts();
        //deleted line
        //next line changed
        radioGroup.setOrientation(LinearLayout.VERTICAL);
        for (UserAccountDetailsModel user : accounts){
            CompanyLocationsModel company = companyLocationSqliteModel.getCompany(user.getCompany_id());
            RadioButton rdbtn = new RadioButton(this);
            rdbtn.setTextSize(17);
            rdbtn.setId(company.getId());
            rdbtn.setText(user.getFirst_name() + " "+user.getLast_name() + " ---- " + company.getName());
            //next line changed
            radioGroup.addView(rdbtn);
        }
        //deleted line
        //next line changed
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
               // checkedId is the RadioButton selected
                Log.i("test", "Checked is "+checkedId);
            }
        });
    }
    
    void displayOpts(){
    List accounts=userAccountDetailsSqliteModel.getAccounts();
    //删除行
    //下一行已更改
    射线组设置方向(线性布局。垂直);
    for(UserAccountDetailsModel用户:帐户){
    CompanyLocationsModel company=companyLocationSqliteModel.getCompany(user.getCompany_id());
    RadioButton rdbtn=新的RadioButton(本);
    rdbtn.setTextSize(17);
    setId(company.getId());
    rdbtn.setText(user.getFirst\u name()+“”+user.getLast\u name()+“----”+company.getName());
    //下一行已更改
    radioGroup.addView(rdbtn);
    }
    //删除行
    //下一行已更改
    radioGroup.setOnCheckedChangeListener(新的radioGroup.OnCheckedChangeListener()
    {
    检查更改后的公共无效(RadioGroup组,int checkedId){
    //checkedId是选中的单选按钮
    Log.i(“测试”、“已检查为”+已检查EDID);
    }
    });
    }
    
    为什么要创建另一个
    RadioGroup
    ,为什么不直接在主
    RadioGroup
    中添加您的all
    RadioButton
    ?删除此
    RadioGroup帐户\u radios=新的RadioGroup(此)
    radioGroup
    中添加所有内容,然后重试。@YaroslavOvdiienko谢谢,现在可以了,我看到了我的愚蠢错误,没关系,下一次只要保持一致思考并保持代码干净)为什么要创建另一个
    RadioGroup
    ,为什么不直接在主
    RadioGroup
    中添加您的all
    RadioButton
    ,为什么要将一个
    RadioGroup
    添加到另一个?删除此
    RadioGroup帐户\u radios=新的RadioGroup(此)
    radioGroup
    中添加所有内容,然后再试一次。@YaroslavOvdiienko谢谢,现在可以了,我看到了我的愚蠢错误,没关系,下次只要保持一致思考,保持代码干净就行了)