Get values from the 4th Firebase node on Android

Asked

Viewed 75 times

1

I need to fetch the red highlighted values in the image, but I only know the blue highlighted item.

Nó no banco

I tried that way:

firebase = ConfiguracaoFirebase.getFirebase().child("tb_contato").child(identificador);
                valueEventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot filho: dataSnapshot.getChildren())
                        {
                            Contato contato = filho.getValue(Contato.class);
                            Log.i("Teste","Teste");
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                };
                firebase.addListenerForSingleValueEvent(valueEventListener);

Where the "identifier" is the blue underlined value of the image.

But when I try to compile it, it returns to me like this: Retorno

  • From what you put in the question, your bank seems to be poorly structured, it is better to avoid nesting data as much as possible. The only way I see it is to loop the keys, save in an array and use it to access the child data

1 answer

0


I decided that way, I don’t know if it’s the right one, but it worked.

firebase = ConfiguracaoFirebase.getFirebase().child("tb_contato").child(identificador);
                valueEventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            for (DataSnapshot filho: dataSnapshot.getChildren())
                            {
                                chaveServico = filho.getKey();
                                firebaseContato = ConfiguracaoFirebase.getFirebase().child("tb_contato").child(identificador).child(chaveServico);
                                valueEventListenerContato = new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        for (DataSnapshot filho: dataSnapshot.getChildren())
                                        {
                                            //===== Altera o nome do usuário no contato =======
                                            String chaveUsuario = filho.getKey();
                                            firebaseUsuario = ConfiguracaoFirebase.getFirebase();
                                            DatabaseReference hopperRef2 = firebaseUsuario.child("tb_contato").child(identificador).child(chaveServico).child(chaveUsuario);
                                            Map<String, Object> hopperUpdates2 = new HashMap<>();
                                            hopperUpdates2.put("cttUsrIDNomeSolicitante", txtNome.getText().toString());
                                            hopperRef2.updateChildren(hopperUpdates2);

                                            firebaseUsuario = ConfiguracaoFirebase.getFirebase();
                                            DatabaseReference hopperRef3 = firebaseUsuario.child("tb_contato").child(chaveUsuario).child(chaveServico).child(identificador);
                                            Map<String, Object> hopperUpdates3 = new HashMap<>();
                                            hopperUpdates3.put("cttUsrNomePrestador", txtNome.getText().toString());
                                            hopperRef3.updateChildren(hopperUpdates3);
                                            //=================================================
                                        }
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                };
                                firebaseContato.addListenerForSingleValueEvent(valueEventListenerContato);
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                };
                firebase.addListenerForSingleValueEvent(valueEventListener);

Browser other questions tagged

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