How to recover data using Firebaserecycleradapter that has automatically generated Ids?

Asked

Viewed 118 times

0

am having an error using Firebaserecycleradapter, at the time of recovering the data from firebase presents this error: "com.google.firebase.database.Databaseexception: Expected a 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 generating a key automatically for each clinic. Can anyone help me with this? How I recover the ids and show all the clinics registered at firebase?

Banco de dados no 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);
}

1 answer

0


I’m new to Firebase, but I think it’s because you need to create an addListenerForSingleValueEvent in order to recover 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) {

        }
    });

}
  • But how’s your database? In my case I am using Firebaserecycleradapter, I do not understand where and how to recover and set the id so that they can be located the data.

  • My database I get by this line " Databasereference tecnicoDb = Firebasedatabase.getInstance(). getReference(). Child("Usuarios"). Child("Clients"). Child(usuarioAtualID). Child("connections"). Child("technicians"); " But since I didn’t use this Firebaserecycleradapter it might be different approach.

  • Where is declared and how do you receive the data of the variable "userAtualID"? My reference is at the root, but there I have Ids, that gives the problem, I can not find a way to make my reference to reach it and read all children of the id generated automatically with push.

  • User private string AtualID; Inside Oncreate: usuarioAtualID = Firebaseuth.getInstance(). getCurrentUser(). getUid(); getCurrentUser(). getUid(); will take the ID of the current logged in user.

  • I understood, it is because in case you are getting the user id logged in the app, in my case I needed to get to the ids, my recyclerview shows nothing.

  • What my code does is: First I create a reference to the database: Databasereference tecnicoDb = Firebasedatabase.getInstance(). getReference(). Child("Usuarios"). Child("Clients"). Child(usuarioAtualID). Child("conexoes"). Child("tecnicos");

  • Then I create an addListenerForSingleValueEvent to go through all that are in the field I searched for in the reference: for (Datasnapshot tecnico: dataSnapshot.getChildren()){ Fetchtecnicoinformation(tecnico.getKey()); } The Fetchtecnicoinformation method will receive all id’s and then I go through the Database Technicians field once again to display in my Recyclerview.

Show 2 more comments

Browser other questions tagged

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