How to add Child node in Child firebase android

Asked

Viewed 129 times

0

I would like to know how I can add a new node inside my Child 88d4c350-d2db-4765-b7da-0652b1278bd7 that is inside Child ETE, without overwriting the two tests?

imagem do meu firebase

public void salvar(){
    try {
        if (idButtom == R.id.btn_im_cx_gord) {
            Ete ete = new Ete();
            ete.setCaixaDeGorduraExistente(spn_st_cx_gordura.getSelectedItem().toString());
            ete.setProjetoCxGord(spn_projeto_sub.getSelectedItem().toString());
            databaseReference = FirebaseDatabase.getInstance().getReference().child("MANUTENÇÃO");
            databaseReference.child(id).setValue(ete);
        }
            databaseReference.child("MANUTENÇÃO").child(id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    Intent intent = getIntent();
                    String id = intent.getStringExtra(MainActivity.Uid);

                    Ete ete = dataSnapshot.getValue(Ete.class);
                    if (ete != null) {
                        ete.setModeloGrad(spn_mod_grad.getSelectedItem().toString());
                        ete.setSituacaoGrad(spn_st_grad.getSelectedItem().toString());
                        ete.setPresenGordGrad(spn_pres_gordura_grad.getSelectedItem().toString());
                        ete.setResProcProdut(spn_res_pros_prod_grad.getSelectedItem().toString());
                        firebaseDatabase.getReference("MANUTENÇÃO").child(id).setValue(ete).toString();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
    } catch (Exception ex){
        MensageBox.Show(this, "Erro", "Erro ao Salvar Dados" + ex.getMessage());
    }
}
  • I migrated to Firestore as soon as possible, because the Realtime Database has some disadvantages, the main one is that there is no document storage, but the storage of a single gigantic document, anyway there is a method for this updateChildren() instead of setValue() take a look at doc https://firebase.google.com/docs/database/android/read-and-write?hl=pt-br

1 answer

0

problem solved

public void salvar(){
        try {
            databaseReference = FirebaseDatabase.getInstance().getReference();
            if (idButtom == R.id.btn_im_cx_gord) {
                Ete ete = new Ete();
                ete.setCaixaDeGorduraExistente(spn_st_cx_gordura.getSelectedItem().toString());
                ete.setProjetoCxGord(spn_projeto_sub.getSelectedItem().toString());
                databaseReference = databaseReference.child("MANUTENÇÃO").child(id);
                databaseReference.setValue(ete);
            } else if (idButtom == R.id.btn_im_grad){
                databaseReference.child("MANUTENÇÃO").child(id).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        Ete ete = dataSnapshot.getValue(Ete.class);
                        if (ete != null) {
                            ete.setModeloGrad(spn_mod_grad.getSelectedItem().toString());
                            ete.setSituacaoGrad(spn_st_grad.getSelectedItem().toString());
                            ete.setPresenGordGrad(spn_pres_gordura_grad.getSelectedItem().toString());
                            ete.setResProcProdutGrad(spn_res_pros_prod_grad.getSelectedItem().toString());
                            firebaseDatabase.getReference("MANUTENÇÃO").child(id).setValue(ete);
                            disableItens.ShowItensScren(2,true, false, initManutencao.this);
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            } else if (idButtom == R.id.btn_im_cest){
                databaseReference.child("MANUTENÇÃO").child(id).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        Ete ete = dataSnapshot.getValue(Ete.class);
                        if (ete != null) {
                            ete.setModeloCest(spn_mod_cesto.getSelectedItem().toString());
                            ete.setSituacaoCest(spn_st_cest.getSelectedItem().toString());
                            ete.setPresenGordCest(spn_pres_gordura_cest.getSelectedItem().toString());
                            ete.setResProcProdutCest(spn_res_pros_prod_cest.getSelectedItem().toString());
                            firebaseDatabase.getReference("MANUTENÇÃO").child(id).setValue(ete);
                            disableItens.ShowItensScren(3,true, false, initManutencao.this);
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

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

Browser other questions tagged

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