Recover firebase data - order items

Asked

Viewed 34 times

0

Good afternoon

I am trying to list all the items of a request in a Recycler view, but the query only returns a value as below :

 public void recuperarItemPedido(String idPedido1) {
    databaseReference = FirebaseConfig.getFirebaseDatabase().child("pedidos");
   Query query = databaseReference.orderByChild("id").startAt(idPedido1).endAt(idPedido1+"\uf8ff");

      query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                pedidoList.add(dataSnapshot1.getValue(Pedido.class));
            }
            Collections.reverse(pedidoList);
            adapterDetalhes.notifyDataSetChanged();
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

on another screen, this same code returns all the items that are within the request. I don’t know what else can be

2 answers

0

Good morning

I was able to resolve by changing the Adapter as follows:

public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    Pedido pedido = pedidoList.get(position);
    List<ItemPedido> itens = new ArrayList<>();
    itens = pedido.getItens();

    String  modelo = "",cor1 = "",cor2= "",cor3= "",cor4= "",valor = "",qtd="",tam = "";
    int numeroItem = 1;

    for ( ItemPedido itemPedido : itens ){
        String modelo1 = itemPedido.getModelo();
        Double preco = itemPedido.getPreco();
        String cor1_1 = itemPedido.getCor1();
        String cor2_2 ="", cor3_3="", cor4_4="";

        String tam1 = itemPedido.getTam();
        int qtd1 = itemPedido.getQuantidade();

        if(itemPedido.getCor2()!=null) {
            cor2_2 = itemPedido.getCor2();
        }if(itemPedido.getCor3()!=null){
            cor3_3 = itemPedido.getCor3();
        }if (itemPedido.getCor4()!=null){
            cor4_4 = itemPedido.getCor4();
        }
        modelo += numeroItem + "\t" + modelo1 +"\n";
        cor1 += "\t"+cor1_1+ "\n";
        cor2 += "\t"+cor2_2+ "\n";
        cor3 += "\t"+cor3_3+ "\n";
        cor4 += "\t"+cor4_4+ "\n";
        qtd += "\t"+qtd1+ "\n";
        tam += "\t"+tam1+ "\n";
        valor +="\t"+preco+ "\n";
        numeroItem++;
    }
    holder.nomeModeloItem.setText(modelo);
    holder.cor1.setText(cor1+cor2+cor3+cor4);
    holder.qtd.setText(qtd);
    holder.tamanho.setText(tam);
    holder.valor.setText(valor);
}

I hope you help someone, thank you very much!

0

Good night. Try instantiating the list in onDataChange again before retrieving the items

pedidoList = new ArrayList();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
        pedidoList.add(dataSnapshot1.getValue(Pedido.class));
    }
  • Good afternoon, I tried that too but it didn’t work...

  • This could be the Firebaseconfig class. It shows her code

Browser other questions tagged

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