Get text (String) from a RadioButton within a RadioGroup

1

I want to get the text of rabiobutton selected by the user. I have 5 radiobutton within a radiogroup to then use this field in an Asynctask and insert in a BD. the fact is that I can not get the text.

What I have is the following

        radioGroup=(RadioGroup) findViewById(R.id.radiog1);
        rbmuybueno=(RadioButton) findViewById(R.id.rbmuybien);
        rbbueno=(RadioButton) findViewById(R.id.rbbueno);
        rbregular=(RadioButton) findViewById(R.id.rbregular);
        rbmalo=(RadioButton) findViewById(R.id.rbmala);
        rbmuymalo=(RadioButton)findViewById(R.id.rbmuymala);
public void  validarradiobutton(){
    if (rbmuybueno.isChecked() || rbbueno.isChecked() || rbmalo.isChecked() || rbregular.isChecked()  || rbmuymalo.isChecked())
    {

            final String pregunta=preguntaa.getText().toString();


    }
    else
    {
        final AlertDialog.Builder alertaDeError2 = new AlertDialog.Builder(CalificarEvento.this);
        alertaDeError2.setTitle("Advertencia");
        alertaDeError2.setMessage("Selecione una alternativa para continuar.");
        alertaDeError2.setPositiveButton("Entendido", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alertaDeError2.create();
        alertaDeError2.show();

    }

I need to get the text inside the IF and then pass the parameters to the Asynctask

    
asked by Ashley G. 11.01.2017 в 21:33
source

1 answer

3

If you have a RadioButton to get the text it would be:

 rbmuybueno=(RadioButton) findViewById(R.id.rbmuybien);
 String texto = rbmuybueno.getText().toString();

If you have a RadioGroup containing RadioButtons , you would first get the id of RadioGroup :

int radioButtonId = radioButtonGroup.getCheckedRadioButtonId();

You would get a reference of the element by means of the id:

View radioButton = radioButtonGroup.findViewById(radioButtonId);

and you would get the index of RadioButton within RadioGroup :

int indice = radioButtonGroup.indexOfChild(radioButton);

To get the RadioButton text:

 RadioButton rb = (RadioButton)  radioButton.getChildAt(indice);
 String texto = rb.getText().toString();
    
answered by 11.01.2017 / 22:03
source