title: How to get data from Firebase Database in a loop?
body: <p>in my code I have a list of products barcode and in my Firebase Database I have values inside this barcodes, like this: <img src="https://i.stack.imgur.com/B9TpL.png" alt="BarCode structure">. </p>
  
  <p>I'm trying to retrieve the barcodes datas in a loop, but I cannot handle with this. Here is my code: </p>
  
  <pre><code>for (int i = 0; i &lt; 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) {
  
                  }
              });
          }
  </code></pre>
  
  <p>This code saves all data in barcode child in a <code>HashMap</code>, but when I try to get this <code>HashMap</code> value using <code>Log.i("listaMercadosCB", String.valueOf(listaMercadosCB));</code>  after the loop, it returns null... </p>
  
  <p>This is the logcat for the values: <code>02-13 19:44:17.146 13903-13903/com.example.mts_rodrigues.projetocartolada I/listaMercadosCB: {wrxYaHZuLBTfrPrrvThxQREFzK02=4.5, d63vvncs2gh6PSnxFAjxhTOayyq1=2.9}</code></p>
  
  <blockquote>
    <p>This is inside the loop</p>
  </blockquote>
  
  <p><code>02-13 19:44:17.066 13903-13903/com.example.mts_rodrigues.projetocartolada I/listaMercadosCB: {}</code></p>
  
  <blockquote>
    <p>This is after de loop</p>
  </blockquote>
  
  <p>I guess that the value outside the loop is called before the Firebase Database retrieved him inside the loop...</p>
  
  <p>All this codes are inside the <code>onCreate()</code> method.
   Anyone can help me ? Thanks!</p>