如何循环从Firebase数据库获取数据? [英] How to get data from Firebase Database in a loop?

查看:98
本文介绍了如何循环从Firebase数据库获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有一个产品条形码列表,在我的Firebase数据库中,我在该条形码中包含值,例如:.

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) {

                }
            });
        }

此代码将所有数据保存在HashMap中的条形码子项中,但是当我尝试在循环后使用Log.i("listaMercadosCB", String.valueOf(listaMercadosCB));来获取此HashMap值时,它将返回null ...

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

这是值的日志记录: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

我想循环外部的值是在Firebase数据库在循环内检索到他之前调用的...

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

所有这些代码都在onCreate()方法内部. 有人可以帮助我吗?谢谢!

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

推荐答案

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

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数据库获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆