How to save form in android studio and send to firebase

Asked

Viewed 502 times

0

I have a simple form, with name, surname and age. I want to send the data entered in editText of this form to firebase. The solutions found on the web smp refer to login forms which leaves the code very complex.

I already created the json file in the firebase called person and the node "name", "last name" and"age" should receive the values of the formulation... if someone can guide me on how to do this I thank.

1 answer

1

You can do the following: Get the firebase reference:

mDatabase = FirebaseDatabase.getInstance().getReference();

Then create the fields into which the data will be entered:

    nomeEdit = (EditText) findViewById(R.id.field_nome);
    sobrenomeEdit = (EditText) findViewById(R.id.field_sobrenome);
    idadeEdit = (EditText) findViewById(R.id.field_idade);

Pick up what was typed in these fields:

    final String nome = nomeEdit.getText().toString();
    final String sobrenome = sobrenomeEdit.getText().toString();
    final String idade = idadeEdit.getText().toString();

Then you can use a Map to enter the data typed in Edittexts.

private void SalvarDados(String userId, String nome, String sobrenome, String idade) {
        String key = mDatabase.child("usuario").push().getKey();

        Post post = new Post(userId, nome, sobrenome, idade);
        Map<String, Object> postValues = post.toMap();

        Map<String, Object> childUpdates = new HashMap<>();
        childUpdates.put("/usuario-dados/" + userId + "/" + key, postValues);

    mDatabase.updateChildren(childUpdates);
}

To display them, just create an Adapter in the main activity of your application.

Browser other questions tagged

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