Search code examples
android-studioswitch-statementradio-groupandroid-radiogroupandroid-radiobutton

Getting text from a radio button that is part of a radio group


These are two methods I have in my class where I hope to return a string of the text attached to each radio button:

public String toppingGroupCheck(RadioGroup toppingGroup, int checkedId){
        switch (checkedId){
            case R.id.toppingRadio1:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
            case R.id.toppingRadio2:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
            case R.id.toppingRadio3:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
            case R.id.toppingRadio4:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
            case R.id.toppingRadio5:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
        }
        return selectedTopping;
    }

    public String sideGroupCheck(RadioGroup sidesGroup, int checkedId){
        switch (checkedId){
            case R.id.sideRadio1:
                checkedId = sidesGroup.getCheckedRadioButtonId();
                selectedSideRad = (RadioButton)findViewById(checkedId);
                selectedSide = selectedSideRad.getText().toString();
            case R.id.sideRadio2:
                checkedId = sidesGroup.getCheckedRadioButtonId();
                selectedSideRad = (RadioButton)findViewById(checkedId);
                selectedSide = selectedSideRad.getText().toString();
            case R.id.sideRadio3:
                checkedId = sidesGroup.getCheckedRadioButtonId();
                selectedSideRad = (RadioButton)findViewById(checkedId);
                selectedSide = selectedSideRad.getText().toString();
        }
        return selectedSide;
    }

Then I have this, which should utilize those methods and return the String to the variables tmpTopping and tmpSide:

public void submitForm(View view){
        Intent submitform = new Intent(this, submitForm.class);

        String tmpTopping = toppingGroupCheck(toppingGroup, checkedId);
        submitform.putExtra("topping",tmpTopping);
        String tmpSide = sideGroupCheck(sidesGroup, checkedId);
        submitform.putExtra("side",tmpSide);
        startActivity(submitform);
    }

Could somebody explain what I'm possibly doing wrong, or a better way of going about his? Thanks.

UPDATE: I tried to do:

    checkedId = toppingGroup.getCheckedRadioButtonId();
    selectedToppingRad = (RadioButton)findViewById(checkedId);
    String topping = selectedToppingRad.getText();

but it kept saying that the "selectedToppingRad.getText()" would return a character sequence instead of a string. Instead, I just inserted the "selectedToppingRad.getText()" right into the putExtra and it worked.


Solution

  • i actually don't know what you're doing with your codes but I think this may help

    Visit Android getting value from selected radiobutton and take a look for answers.

    why not just to get the id then get it's text? they have unique id's even in the same group. Like the comment there by @Turuu; but instead toast, put it in string

    radioButton = (RadioButton) findViewById(checkedId);
    String radioButtonText = radioButton.getText();
    

    I'ts just a sample anyway