How to make a button record data from a form with click events

Asked

Viewed 683 times

2

I have a form and wish to save it when the button is pressed salvage, the solution I made is not working.

public class SalvarFormulario extends AppCompatActivity {


EditText Nome;
EditText edtTelefone;
EditText edtObservacao;
EditText edtEmail;
Button btSalvar;


FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_formulario);


    Nome = (EditText) findViewById(R.id.Nome);
    edtEmail = (EditText) findViewById(R.id.edtEmail);
    edtObservacao = (EditText) findViewById(R.id.edtObservacao);
    edtTelefone = (EditText) findViewById(R.id.edtTelefone);
    btSalvar = (Button) findViewById(R.id.btSalvar);






    InicializaFirebase();
    SalvarDados(); // <--- salva os dados 
}




private void InicializaFirebase() {
    FirebaseApp.initializeApp(SalvarFormulario.this);
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference();
}

private void limparCampos() {
    Nome.setText("");
    edtTelefone.setText("");
    edtObservacao.setText("");
    edtEmail.setText("");

}
  • You must create the method SalvarDados() if it hasn’t been created yet, you can’t tell because the code isn’t complete.

  • already created: public void Save Data() { // if (btSalvar.isSelected()) { Register c = new Register(); c.setuid(UUID.randomUUID().toString(); c.setName(Name.gettext().toString(); c.setTelephone(edtTelephone.gettext().toString(); c.setEmail(edtEmail.gettext().toString()); c.setObservation(edtObservation.gettext().toString()); databaseReference.Child("Registration"). Child(c.getUid()). setValue(c); cleanCampos(); }

  • Then see if the answer given by @Natanfelipe solves your question.

1 answer

1

You have to grab your button and use the setOnClickListener method and there you call the method responsible for saving the data, this way:

         botao.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             salvarDados();
         }
     });

Browser other questions tagged

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