Recover Firebase data with multiple child

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 at the time of bringing the data of Firebase , I can not use the correct structure to bring the user id, then the node "protocol", then the company, then the subject with their information. I'm using this structure to bring information from Firebase , and with this my Listview does not display anything:

    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:

    
asked by anonymous 06.01.2018 / 00:14

1 answer

0

You are initializing the adapter multiple times within the forEach loop. Initialize 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) {

        }


    });
    
25.02.2018 / 15:03