How to get value from a RadioButon inside a RadioGroup to save in the bank

0

I wanted to know how I get the value of a RadioButton to save it to the sqlite bank. In the example below the instructor does as follows:

public class FormularioHelper {

private Aluno aluno;

public FormularioHelper(FormularioActivity activity) {
    campoNome = (EditText) activity.findViewById(R.id.formulario_nome);
    campoEndereco = (EditText) activity.findViewById(R.id.formulario_endereco);
    campoTelefone = (EditText) activity.findViewById(R.id.formulario_telefone);
    campoSite = (EditText) activity.findViewById(R.id.formulario_site);
    campoNota = (RatingBar) activity.findViewById(R.id.formulario_nota);
    campoFoto = (ImageView) activity.findViewById(R.id.formulario_foto);
    aluno = new Aluno();
}

public Aluno pegaAluno() {
    aluno.setNome(campoNome.getText().toString());
    aluno.setEndereco(campoEndereco.getText().toString());
    aluno.setTelefone(campoTelefone.getText().toString());
    aluno.setSite(campoSite.getText().toString());
    aluno.setNota(Double.valueOf(campoNota.getProgress()));
    aluno.setCaminhoFoto((String) campoFoto.getTag());
    return aluno;
}}

Mine is partially like this:

public class FormularioProcessoHelper {
    private ImageView ivCaminhoFoto;
    private Spinner spPromotores;
    private RadioButton rbFavoravel;
    private RadioButton rbDesfavoravel;
    private RadioGroup radioGroup;
    private EditText edtObservacao;
    private Button btnDataProcesso;

    private Processo processo;

    public FormularioProcessoHelper(FormularioProcessoActivity activity) {
        ivCaminhoFoto = (ImageView) activity.findViewById(R.id.iv_foto_selecionada);
        spPromotores = (Spinner) activity.findViewById(R.id.spinner_promotores);
        rbFavoravel = (RadioButton) activity.findViewById(R.id.posicao_judicial_favoravel);
        rbDesfavoravel = (RadioButton) activity.findViewById(R.id.posicao_judicial_desfavoravel);
        edtObservacao = (EditText) activity.findViewById(R.id.edt_observacao);
        btnDataProcesso = (Button) activity.findViewById(R.id.btn_data);

        processo = new Processo();
    }

    public Processo pegaProcessoPraCadastro() {
        Processo processo = new Processo();
        processo.setCaminhoFoto((String) ivCaminhoFoto.getTag());
        processo.setPosicao(radioGroup.); // AQUI, QUERIA PEGAR O VALOR DO RADIOBUTTON SELECIONADO
        processo.setPromotor(spPromotores.getSelectedItemPosition()); //AQUI --//-- SPINNER SELECIONADO
        processo.setDataProcesso(btnDataProcesso.PegarAData()); // AQUI --//-- A DATA SETADA PELO USUARIO
        processo.setObservacao(edtObservacao.getText().toString());
        return processo;
    }
}

The problem is, I did not find the methods to get these values. Both Spinner and RadioButton's. The commented lines are where the errors are!

My model class Process:

public class Processo implements Serializable {
    private Long id;
    private String caminhoFoto;
    private Usuario promotor;
    private String observacao;
    private String dataProcesso;
    private PosicaoJudiciario posicao;

    public Usuario getPromotor() {
        return promotor;
    }

    public void setPromotor(Usuario promotor) {
        this.promotor = promotor;
    }

    public String getObservacao() {
        return observacao;
    }

    public void setObservacao(String observacao) {
        this.observacao = observacao;
    }

    public String getDataProcesso() {
        return dataProcesso;
    }

    public void setDataProcesso(String dataProcesso) {
        this.dataProcesso = dataProcesso;
    }

    public PosicaoJudiciario getPosicao() {
        return posicao;
    }

    public void setPosicao(PosicaoJudiciario posicao) {
        this.posicao = posicao;
    }

    public String getCaminhoFoto() {
        return caminhoFoto;
    }

    public void setCaminhoFoto(String caminhoFoto) {
        this.caminhoFoto = caminhoFoto;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Nome: " + getPromotor();
    }
}

My User Model class:

public class Usuario implements Serializable{

    private String nome;
    private String senha;
    private String email;
    private String matricula;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }
}
    
asked by anonymous 27.09.2017 / 23:05

2 answers

2

You need to get the radio button by the id, then get the text value of that button. Try this code below.

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.youradio);
String radiovalue =((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId())).getText().toString(); 

Routine to save and load:

private PREF_RADIO_BUTTON = "PREF_RADIO_BUTTON"
private RADIO_BUTTON = "RADIO_BUTTON"

public static void saveSetting(Context context, String key, String value) {
        SharedPreferences mSharedPreferences = context.getSharedPreferences(PREF_RADIO_BUTTON, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String loadSetting(Context context, String key, String defvalue) {
        SharedPreferences mSharedPreferences = context.getSharedPreferences(PREF_RADIO_BUTTON, Context.MODE_PRIVATE);
        return mSharedPreferences.getString(key, defvalue);
    }

   public static void saveRadioButton(Context context, String value) {
        saveSetting(context, RADIO_BUTTON, value);
    }

    public static String loadRadioButton(Context context) {
        return loadSetting(context, RADIO_BUTTON ,"");
    }

Saving / loading preference:

saveRadioButton(getContext(),radiovalue);
loadRadioButton(getContext())
    
28.09.2017 / 04:41
0

Do the following, create a field in the database parecerFavoravel BOOLEAN; in the bank and True to Favorable and False to unfavorable.

Declare a Listener on RadioGroup to capture any action on it as Example:

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(RadioGroup group, int checkedId) {
             //Verifica qual RB foi selecionado
             if(checkedId == R.id.posicao_judicial_favoravel) {
                 //Radio favorável Selecionado!
                objeto.setParecerFavoravel(true);
             } else if(checkedId == R.id.posicao_judicial_desfavoravel) {
                 //Radio desfavorável Selecionado!
                objeto.setParecerFavoravel(false);
             }
         }

     });

Any questions about capturing the click on RadioGroup, check out this site: link

Dai is just have to write the object in the bank normally. If this opinion is from the process, it creates this field in the process DTO and in the database table of the process.

    
29.09.2017 / 19:35