Search code examples
javaandroidandroid-radiobuttonandroid-radiogroup

RadioGroup on click fails


I have set up a radio group dynamically like this:

In my XML I have:

<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">

In my Java code, I have

RadioGroup radioGroup;
protected void onCreate(){
    radioGroup = (RadioGroup) findViewById(R.id.user_accounts_radios);
    setupUsers();
}

on the setupUsers

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

The above displays the RadioGroup but I have the following issues:

  1. Whenever I click on the second RadioButton the first one cannot be clicked again

  2. the log on the selected listener doesn't log

I dont know what is wrong, because the RadioButtons are displayed correctly.


Solution

  • You are creating a new RadioGroup which you put into the existing one. This is wrong. Just use the existing one like this:

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