第一次快照后 ArrayList 变空 [英] ArrayList gets empty after first snapshot

查看:29
本文介绍了第一次快照后 ArrayList 变空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个 android 应用程序中使用 firebase 并且发生了一些奇怪的事情,在我的示例中我需要两个快照,1 将用户放入列表中(我使用此快照来填充带有键的字符串数组列表)user)和另一个与用户进行比较,奇怪的行为是我的arraylist在第一个快照后为空,我使用logcat检查它并且第一个快照中的Log返回我1作为arraylist的大小,第二个返回我是0,不知道怎么又变成0了.

I'm working with firebase in a android application and something strange ocurred, in my example I need two snapshots, 1 to get the users inside a list ( I use this snapshot to fill a arraylist of strings with the key of the user) and the other to compare to the users, the strange behavior is that my arraylist is empty after the first snapshot, I used logcat to check it and that Log inside the firstsnapshot returns me 1 as the size of the arraylist, the second returns me 0, dunno how it gets 0 again.

这是我的代码:

 private void prepareFriendList() {
        myRef.child(id).child("FriendLists").child(group).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String keyUser;
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Log.d("number:",snapshot.getKey());
                    keyUser = snapshot.getKey();
                    currentFriends.add(keyUser);
                    Log.d("hello",String.valueOf(currentFriends.size()));

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        for(String s: currentFriends){
            Log.d("idddd",s);
        }

        Log.d("hello",String.valueOf(currentFriends.size()));



        myRef.child(id).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    User user = snapshot.getValue(User.class);

                    for(String item: currentFriends){
                        if (snapshot.getKey().equals(item)) {
                            usersList.add(user);
                        }
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        mAdapter.notifyDataSetChanged();
    }

我不明白为什么会这样,因为我在数组列表中添加了键,有什么提示吗?

I don't understand why that happens, since I'm adding the key inside the arraylist, any tip?

推荐答案

发生这种情况是因为 onDataChange 是异步调用的.这意味着将用户添加到 list 的语句是在调用 onDataChange 之前执行的.这就是为什么您的 list 在该方法之外是空的.因此,为了使用该列表,您需要在 onDataChange() 方法中使用它.

This is happening because onDataChange is called asynchronously. This means that the statement that adds users to the list is executed before onDataChange has been called. That's why your list is empty outside that method. So in order to use that lists, you need to use it inside the onDataChange() method.

对于其他方法,请访问此帖子和此post.

For other approach, please visit this post and this post.

希望有帮助.

这篇关于第一次快照后 ArrayList 变空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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