Insert data into firebase from Alertdialog

Asked

Viewed 147 times

0

Guys I’m having a question, I was wondering if you have how to store data in firebase from a custom Alertdialog?

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

Obg to all who are determined to help

  • Eder, you’ve saved data in firebase before, if so, how does ? use a model class for this ?

  • Opa Eduardo, yes I’ve saved in other projects but it was simple things

1 answer

1


Well, assuming you’ve already created your Alert or know how to create it because you haven’t posted any code, you can do the following: Assuming the user enters some data in edits, and Alert has a Positive button that will save this information and a model class that will work this data, you will call the model class, which in this example is a user class. In the Model class, in addition to the variables and the getters and setters you will have this method:

public void salvar(){

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

Now it is enough that you in the positiveButton method of Alert 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 model class, sending data when it is clicked on the positive Alert button in this way:

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 is your creitério although I recommend using the model classes to make the work better organized. Any doubt I’m available!

  • Cara you are a genius, I understood your explanation straight I will implement here and then I send the result, It was bad not to have posted the code of Alertdialog because I found it simple to implement in my app so it did not even cross my mind to post the question, very obg Brothers.

  • Wow, I’m glad I could help !

Browser other questions tagged

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