如何在循环中从Firebase数据库获取数据?

时间:2022-08-11 16:59:31

in my code I have a list of products barcode and in my Firebase Database I have values inside this barcodes, like this: 如何在循环中从Firebase数据库获取数据?.

在我的代码中,我有一个产品条形码列表,在我的Firebase数据库中,我在这个条形码中有值,如下所示:。

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...

此代码将条形码子中的所有数据保存在HashMap中,但是当我尝试使用Log.i(“listaMercadosCB”,String.valueOf(listaMercadosCB))获取此HashMap值时;在循环之后,它返回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}

这是值的logcat: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: {}

02-13 19:44:17.066 13903-13903 / com.example.mts_rodrigues.projetocartolada I / listaMercadosCB:{}

This is after de loop

这是在de loop之后

I guess that the value outside the loop is called before the Firebase Database retrieved him inside the loop...

我想在Firebase数据库在循环中检索到它之前调用循环外的值...

All this codes are inside the onCreate() method. Anyone can help me ? Thanks!

所有这些代码都在onCreate()方法中。有人可以帮帮我吗?谢谢!

1 个解决方案

#1


0  

This is not the correct way in which you can use the data that you get from a Firebase Realtime database. You are guessing right. onDataChange() method has an asynchronous behaviour, which means that your data si not available until this method is executed. A quick solution would be to use that data only inside onDataChange() method, or if you want to use it outside, dive in the asynchronous world and use the last part of my answer from this post.

这不是您使用从Firebase Realtime数据库获取的数据的正确方法。你猜对了。 onDataChange()方法具有异步行为,这意味着在执行此方法之前,您的数据不可用。一个快速的解决方案是仅在onDataChange()方法中使用该数据,或者如果您想在外部使用它,请深入了解异步世界并使用本文中我的答案的最后一部分。

#1


0  

This is not the correct way in which you can use the data that you get from a Firebase Realtime database. You are guessing right. onDataChange() method has an asynchronous behaviour, which means that your data si not available until this method is executed. A quick solution would be to use that data only inside onDataChange() method, or if you want to use it outside, dive in the asynchronous world and use the last part of my answer from this post.

这不是您使用从Firebase Realtime数据库获取的数据的正确方法。你猜对了。 onDataChange()方法具有异步行为,这意味着在执行此方法之前,您的数据不可用。一个快速的解决方案是仅在onDataChange()方法中使用该数据,或者如果您想在外部使用它,请深入了解异步世界并使用本文中我的答案的最后一部分。