How to Iterate on Datasnapshot correctly in Firebase?

Asked

Viewed 286 times

0

I am 8 days trying to get from firebase the records I put there. However, I can never bring them properly.

--- Android for Firebase

   private void initPreencheLista() {
        listCelula.clear();
        novaRef = databaseReference.child( "Celulas" );
        novaRef.addValueEventListener( new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Celula c = new Celula();
                for(DataSnapshot objSnapshot:dataSnapshot.getChildren()){
                    c = objSnapshot.getValue(Celula.class);
                    cels.add( c.getCelula() );
                }
                arrayAdapterCelula = new ArrayAdapter<String>(CelulasActivity.this,android.R.layout.simple_list_item_1, cels );
                celulaList.setAdapter( arrayAdapterCelula );
            }

I tried so many ways to do that I even used

   for(DataSnapshot objSnapshot:dataSnapshot.getChildren()){
                 String a = objSnapshot.child( "Celula" ).getValue(String.class);
                 cels.add( a );

I just can’t give up. I’m in the fight, if anyone can help me I’ll be grateful.

In the first way the objSnapshot comes loaded with all the object I called. Error: com.google.firebase.database.Databaseexception: Can’t Convert Object of type java.lang.String to type cellulae.Cellula.

1 answer

0


How is your node arranged in firebase? For example, if your node is with two keys you will need to traverse the data with two for. One for the first key and one for the second key. Something like this:

inserir a descrição da imagem aqui

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {                
                Log.i("retorno", dataSnapshot.getValue().toString());
                //Estrutura: Id1, Id2, então são 2 for.

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    for(DataSnapshot dados : ds.getChildren()){
                        Usuario user = dados.getValue(Usuario.class);
                        idUsuario = usuario.getIdUsuario();                           
                        Log.i("Escolhido", "Id: " + idUsuario);

                    }
                }
  • I did so because I only need one for. But it was a mistake. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&#xA; for(DataSnapshot objSnapshot:dataSnapshot.getChildren()){&#xA; Celula c = objSnapshot.getValue(Celula.class);&#xA; String celula = c.getCelula();&#xA; cels.add( celula ); com.google.firebase.database.Databaseexception: Can’t Convert Object of type java.lang.String to type cellulae. Cellula

  • private void initPreencheList() { listCelula.clear(); novaRef = databaseReference.Child( "Cells" );

  • 1

    Dude! I did it again as Iassuli made two gift for and it worked. I can’t believe it. Thank you. They should put that in the Firebase documentation. I find incomplete.

Browser other questions tagged

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