Record data in different child classes Android Firebase

Asked

Viewed 63 times

0

I’m trying to record some data in another child class in the firebase, but without success, follows code. However unsuccessfully, when I run the application first the client data is recorded, and soon after the data is overwritten the client data.

private void salvar() {
  Cliente cliente = new Cliente();
  Ete ete = new Ete();
  try {
    cliente.setUid(UUID.randomUUID().toString());
    cliente.setNome(in_nome_empresa.getText().toString());
    cliente.setCnpj(in_cnpj.getText().toString());
    cliente.setEndereco(in_endereco.getText().toString());
    cliente.setCidade(in_cidade.getText().toString());
    cliente.setCep(in_cep.getText().toString());
    cliente.setEstado(spn_estado.getSelectedItem().toString());
    databaseReference.child(String.valueOf(maxId + 1)).setValue(cliente);

    ete.setFabricante(spn_fabricante.getSelectedItem().toString());
    ete.setModelo(spn_ModEte.getSelectedItem().toString());
    databaseReference.child(String.valueOf(maxId + 1)).setValue(ete);

  } catch (Exception ex) {
    MensageBox.Show(this, "Erro", "Erro ao Salvar Dados" + ex.getMessage());
  }
}
  • That’s because you’re writing in the same reference (databaseReference.child(String.valueOf(maxId + 1))), what you’re trying to do?

  • thank you, I managed to resolve by adding databaseReference = Firebasedatabase.getInstance(). getReference(). Child("ETE"); before the ETE setvalue

1 answer

0

private void salvar(){

            Cliente cliente = new Cliente();
            Ete ete = new Ete();
        try {

            cliente.setUid(UUID.randomUUID().toString());
            cliente.setNome(in_nome_empresa.getText().toString());
            cliente.setCnpj(in_cnpj.getText().toString());
            cliente.setEndereco(in_endereco.getText().toString());
            cliente.setCidade(in_cidade.getText().toString());
            cliente.setCep(in_cep.getText().toString());
            cliente.setEstado(spn_estado.getSelectedItem().toString());
            databaseReference = FirebaseDatabase.getInstance().getReference().child("CLIENTE");
            databaseReference.child(String.valueOf(maxId + 1)).setValue(cliente);

            ete.setFabricante(spn_fabricante.getSelectedItem().toString());
            ete.setModelo(spn_ModEte.getSelectedItem().toString());
            databaseReference = FirebaseDatabase.getInstance().getReference().child("ETE");
            databaseReference.child(String.valueOf(maxId + 1)).setValue(ete);

          } catch (Exception ex){
            MensageBox.Show(this, "Erro", "Erro ao Salvar Dados" + ex.getMessage());
        }
    }

solved.

Browser other questions tagged

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