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

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

问题描述

我在android应用程序中使用firebase和一些奇怪的事情,在我的例子中我需要两个快照,1让用户在列表中(我使用这个快照来填充字符串的数组列表用户),另一个与用户进行比较,奇怪的行为是我的数组列表第一次快照后是空的,我用logcat来检查它,并且第一次快照里面的Log返回1作为数组列表的大小,第二次返回我0,不知道它是如何得到0。



这是我的代码:

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



@Override
public void onCancelled(DatabaseError databaseError){

}
} );

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


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


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

用户用户= snapshot.getValue(User.class);

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

$ b @Override
public void onCancelled(DatabaseError databaseError){


});

mAdapter.notifyDataSetChanged();



$ b我不明白为什么会发生这种情况,里面的arraylist,任何提示?

解决方案

这是因为onDataChange异步调用。这意味着将用户添加到 list 的语句会在调用 onDataChange 之前执行。这就是为什么你的 list 在该方法之外是空的。所以为了使用这个列表,你需要在 onDataChange()方法中使用它。



的方法,请访问这个文章和这个 post

希望它有帮助。 b $ b

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.

Here is my code:

 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?

解决方案

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.

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

Hope it helps.

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

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