Recover Data from Firebase with Multiple Child

Asked

Viewed 221 times

0

I’m using this structure to save the data:

    private void salvar() {
    Protocolo protocolo = new Protocolo();
    protocolo.setAssunto(edtAssunto.getText().toString());
    protocolo.setnProtocolo(edtNProtocolo.getText().toString());
    protocolo.setNomeEmpresa(edtNomeEmpresa.getText().toString());

  databaseReference.child(user.getUid()).child("Protocolos").child(protocolo
       .getNomeEmpresa()).child(protocolo.getAssunto()).setValue(protocolo);
        limparCampos();
    }

but when it comes to bringing in the data from Firebase, I cannot use the correct structure to bring the user id, then the "protocol" node, then the company, then the subject with your information. I’m using this structure to bring you information from Firebase, and with it mine Listview displays nothing:

    private void inicia() {
    inicializaFirebase();
    final ListView listView = (ListView) findViewById(R.id.lista_protocolos);
    final List<Protocolo> listaProtocolo = new ArrayList<>();

        databaseReference.child(user.getUid()).child("Protocolos").child("aqui preciso por a empresa")
    .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot objSnapshot : dataSnapshot.getChildren()) {
                Protocolo protocolos = objSnapshot.getValue(Protocolo.class);
                listaProtocolo.add(protocolos);
                ArrayAdapter<Protocolo> adapter = new ArrayAdapter<>
                        (ProtocolosActivity.this, android.R.layout.simple_list_item_1, listaProtocolo);
                listView.setAdapter(adapter);
            }
            if (listaProtocolo.size() < 1) {
                alert("Você ainda não tem nenhum protocolo cadastrado");
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


    });
}

what should I do?

this is the structure of Firebase: inserir a descrição da imagem aqui

  • Why are you using setValue(protocol)?

  • Protocolo is my instance. ai o setValue is saving my object protocolo given in the Firebase

  • 1

    You have to go in pieces or it’s complicated. First: before doing this: listProtocolo.add(protocols); can you print out any protocol data? Or not?. Second: the datareference you are setting reaches where? You can print with Log. d and debug to see how far you can get in the Firebase structure.

  • You can show the class Protocolo?

1 answer

0

You are initiating the Adapter several times within the cycle forEach. Start only once when the cycle ends:

databaseReference.child(user.getUid()).child("Protocolos").child("aqui preciso por a empresa")
    .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot objSnapshot : dataSnapshot.getChildren()) {
                Protocolo protocolos = objSnapshot.getValue(Protocolo.class);
                listaProtocolo.add(protocolos);
            }
            if (listaProtocolo.size() < 1) {
                alert("Você ainda não tem nenhum protocolo cadastrado");
            }
            else
            {
                ArrayAdapter<Protocolo> adapter = new ArrayAdapter<>
                    (ProtocolosActivity.this, android.R.layout.simple_list_item_1, listaProtocolo);
                listView.setAdapter(adapter);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


    });

Browser other questions tagged

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