从Firebase数据库获取数据后ArrayList显示为空 [英] ArrayList is showing empty after getting data from firebase database

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

问题描述

我正在我的应用程序中实现Firebase.请参考下图作为我的数据库的参考.

I am implementing firebase in my app. Please refer the below image as a reference to my database.

我有一个SLC名称数组,我需要生成一个SLC密钥的arrayList.所以我试图创建特定SLC密钥的arrayList.请一次查看以下代码.

I have an array of SLC names and I need to generate an arrayList of SLC keys. So I am trying to create an arrayList of specific SLC keys. Please see the below code once.

for (int i = 0; i < arr.size(); i++)
{
    Query query = ref.child(pref.getString("groupSelectedDCU", "") + "/" + "DeviceList")
                     .orderByChild("name")
                     .equalTo(arr.get(i));
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child: dataSnapshot.getChildren())
            {
                Log.d("arrID", child.getKey() + "");
                arrID.add(child.getKey());
            }

            Log.d("arrID", Arrays.toString(arrID.toArray()));
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
        }
    });
}
Log.d("arrID", Arrays.toString(arrID.toArray()));

在这里,我尝试将所有SLC密钥添加到arrID数组列表中.但是在访问arrayList时,它返回一个空数组.我是否缺少某些内容,还是必须添加其他听众?需要您的帮助.

Here I am trying to add all the SLC keys into the arrID arraylist. But while accessing the arrayList, it returns an empty array. Am I missing something or do I have to add any more listeners? Need your help.

推荐答案

您误解了 ValueEventListener 方法将是当查询响应最终从网络到达时,在后台称为异步.这样,当最后一条语句运行时(在 for循环之后):

You misunderstood how addListenerForSingleValueEvent works. The provided ValueEventListener methods will be called asynchronously in the background, when the query response finally arrives from the network. As such, when this last statement runs (after the for loop):

...
Log.d("arrID", Arrays.toString(arrID.toArray()));

arrID将仍然为空,因为尚未调用任何onDataChange方法.

the arrID will still be empty since none of the onDataChange methods has been called yet.

替代解决方案.如果您需要等到所有查询完成后,请尝试以下操作:

Alternative solution. If you need to wait until all queries are done, try this instead:

List<String> arrID = ArrayList<String>();
int remainingQueries = arr.size(); 
for (int i = 0; i < arr.size(); i++) {
    Query query = ...
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child: dataSnapshot.getChildren()) {
                Log.d("arrID", child.getKey() + "");
                arrID.add(child.getKey());
            }
            Log.d("arrID", Arrays.toString(arrID.toArray()));
            if (--remainingQueries == 0) onQueriesDone(arrID);
        }
        @Override
        public void onCancelled(FirebaseError firebaseError) { }
    });
}

完成后将调用此方法:

private void onQueriesDone(List<String> arrID) {
    Log.d("arrID", Arrays.toString(arrID.toArray()));
}

尤其是,由于所有Firebase回调均在主线程上触发,因此我们不需要同步锁即可访问共享计数器remainingQueries.

In particular, we don't need synchronization locks to access the shared counter remainingQueries since all Firebase callbacks are triggered on the main thread.

这篇关于从Firebase数据库获取数据后ArrayList显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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