in my code I have a list of products barcode and in my Firebase Database I have values inside this barcodes, like this: .
I'm trying to retrieve the barcodes datas in a loop, but I cannot handle with this. Here is my code:
for (int i = 0; i < listaBarCodeProdutos.size() ; i ++ ){
String barCode = (String) listaBarCodeProdutos.get(i);
Query query = produtosRef.orderByKey().equalTo(barCode);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
//Limpa o hash map
listaMercadosCB.clear();
//Salva os mercados e os preços e um hashmap
listaMercadosCB = (HashMap) snapshot.child("mercados").getValue();
//This is to parse the HashMap keys to Array
Object[] listaIdsCB = listaMercadosCB.keySet().toArray();
//add esse obj em um array de objetos
listaCB.add(listaIdsCB);
Log.i("listaCB1", String.valueOf(listaCB));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
This code saves all data in barcode child in a HashMap
, but when I try to get this HashMap
value using Log.i("listaMercadosCB", String.valueOf(listaMercadosCB));
after the loop, it returns null...
This is the logcat for the values: 02-13 19:44:17.146 13903-13903/com.example.mts_rodrigues.projetocartolada I/listaMercadosCB: {wrxYaHZuLBTfrPrrvThxQREFzK02=4.5, d63vvncs2gh6PSnxFAjxhTOayyq1=2.9}
This is inside the loop
02-13 19:44:17.066 13903-13903/com.example.mts_rodrigues.projetocartolada I/listaMercadosCB: {}
This is after de loop
I guess that the value outside the loop is called before the Firebase Database retrieved him inside the loop...
All this codes are inside the onCreate()
method.
Anyone can help me ? Thanks!