How to take value from a Radiobuton within a Radiogroup to save in the bank

Asked

Viewed 2,660 times

0

wanted to know how I took the value of a Radiobutton to save it in 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 didn’t find the methods to get these values. Both Spinner and Radiobutton’s. The commented lines are where the bugs 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;
    }
}

2 answers

2

You need to grab the radio button by the id then get the value of the text from 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 of the preference:

saveRadioButton(getContext(),radiovalue);
loadRadioButton(getContext())
  • Says Edson, all right? So this part helped, but I wanted to know how I saved in the bank, using that method of mine up there.

  • In device database? You can use Sharedpreferences.

  • I edited my answer, see if it helps you.

  • You want to save the object, radiogroup position or selected value?

  • I want to save its value, it can be either its ID or its Text. Saving in the bank, ta of good size.

  • came to test the example that I complemented the up? it saves in the internal bank.

Show 1 more comment

0

Do this, create a field in the bank parecerFavoravel BOOLEAN; in the bank and True arrow to Favorable and False to Unfavorable.

Declare a Listener in Radiogroup to capture any action on it as the 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 regarding the Radiogroup click capture, check this site: https://examples.javacodegeeks.com/android/core/ui/radiogroup/android-radiogroup-example/

So just have the object saved to the database normally. If this opinion is from the process, create this field in the process’s DTO and in the database’s Table.

  • Now that I see that you already have a guy on the bench for this, leave him BOOLEAN type and record in him as shown above.

  • You could post the Posicaojudiciario class if it exists?

  • Speak Erivaldo, I took this class Posicaojudiciario, I chose to use a String even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.