How to recover data using FirebaseRecyclerAdapter that has automatically generated IDs?

0

I have an error using FirebaseRecyclerAdapter, at the time of retrieving the data the firebase has this error: " com.google.firebase.database.DatabaseException: Expected to List while deserializing, but got a class java.util. HashMap "

I saw that it is something related to reading the ID created for each clinic, I am automatically generating a key for each clinic. Can anyone help me with this? How do I retrieve the ids and display all the registered clinics on the firebase?

mFirebaseDatabase=FirebaseDatabase.getInstance();rootReference=mFirebaseDatabase.getReference().child("clinicas");

    listaClinicas = new ArrayList<>();

}


@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<Clinicas, ViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Clinicas, ViewHolder>(
                    Clinicas.class,
                    R.layout.adapter_lista_clinicas,
                    ViewHolder.class,
                    rootReference

            ) {
                @Override
                protected void populateViewHolder(final ViewHolder viewHolder, final Clinicas clinicas, int position) {

                    final String clinicasIds = getRef(position).getKey();

                    rootReference.child(clinicasIds).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            viewHolder.setDetails(getApplicationContext(), clinicas.getNomeClinica(), clinicas.getEnderecoClinica(),
                                    clinicas.getBairroClinica(), clinicas.getEstadoCidadeClinica(), clinicas.getTelefoneClinica(), clinicas.getWhatsappClinica(),
                                    clinicas.getFoto2());
                        }

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

                        }
                    });

                }
            };


    mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
    
asked by anonymous 12.10.2018 / 01:02

1 answer

0

I'm new to Firebase, but I think it's because you need to create an addListenerForSingleValueEvent to be able to retrieve this information. As in my example below:

private void getUsuarioTecnicoId() {

    DatabaseReference tecnicoDb = FirebaseDatabase.getInstance().getReference().child("Usuarios").child("Clientes").child(usuarioAtualID).child("conexoes").child("tecnicos");
    tecnicoDb.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                for (DataSnapshot tecnico: dataSnapshot.getChildren()){
                    FetchtecnicoInformation(tecnico.getKey());
                }
            }
        }

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

        }
    });

}

private void FetchtecnicoInformation(final String key) {
    DatabaseReference usuarioDb = FirebaseDatabase.getInstance().getReference().child("Usuarios").child("Tecnicos").child(key);
    usuarioDb.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                String usuarioId = dataSnapshot.getKey();
                String nome = "";
                String profissao = "";
                String imagemPerfilUrl = "";

                if (dataSnapshot.child("nome").getValue()!=null){
                    nome = dataSnapshot.child("nome").getValue().toString();
                }

                if (dataSnapshot.child("profissao").getValue()!=null){
                    profissao = dataSnapshot.child("profissao").getValue().toString();
                }

                if (dataSnapshot.child("imagemPerfilUrl").getValue()!=null){
                    imagemPerfilUrl = dataSnapshot.child("imagemPerfilUrl").getValue().toString();
                }


                TecnicosObject obj = new TecnicosObject(usuarioId, nome, profissao, imagemPerfilUrl);
                resultmTecnicos.add(obj);
                mTecnicosAdapter.notifyDataSetChanged();

            }
        }

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

        }
    });

}
    
12.10.2018 / 04:37