Insert data into firebase from AlertDialog

0

I'm wondering if there is a way to store data in the firebase from a custom AlertDialog?

If yes I wanted the help of you to create a logic and to make that idea happen, I think that for those who are starting it would help a lot.

Obg to all who are determined to help

    
asked by anonymous 18.10.2017 / 03:30

1 answer

1

Well, based on the principle that you have already created your alert or know how to create because you have not posted any code, you can do the following: Assuming that the user enters some data in edits, and the alert has a positive button that will save this information and a model class that will work with this data, you will call the model class, which in this example is a class of users. In the Model class, in addition to the variables and getters and Setters you will have this method:

public void salvar(){

    DatabaseReference banco = FirebaseDatabase.getInstance().getReference();
    banco.child("USUARIOS").push().setValue(this);
}

Now it's enough for you in the positiveButton method of alert to create the following call:

Usuarios usuarios = new Usuarios();
usuarios.setNome(editNome.getText().toString());
usuarios.setEmail(editEmail.getText().toString());
usuarios.setTelefone(editTelefone.getText().toString());
usuarios.salvar();
dismiss();//para fechar o alert

Or you can do this update without the template class, sending given by die when the positive button of the alert is clicked like this:

DatabaseReference banco = FirebaseDatabase.getInstance().getReference();
    banco.child("USUARIOS").push().setValue("nome", etNome.getText().toString())
    .setValue("email", etEmail.getText().toString());

The two options should work, there it is to your creitério although I recommend to use the models classes to leave the work better organized. Any questions are available!

    
18.10.2017 / 04:02