无法访问onchildEventListener firebase方法

时间:2016-06-14 15:26:53

标签: android firebase firebase-realtime-database

我正在开发一个使用Firebase作为后端的Android聊天应用。

我在onChildEventListener方法中使用queryFireChatUsers()方法来查询我的用户。问题是屏幕只显示进度条,我不在方法onChildAdded()内。

感谢您的帮助。

这是我的代码。

    showProgressBarForUsers();



    mListenerUsers=mFireChatUsersRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            Log.d(TAG, "inside onChildAdded");
            //Hide progress bar
            hideProgressBarForUsers();

            if(dataSnapshot.exists()){
                //Log.e(TAG, "A new user was inserted");

                String userUid=dataSnapshot.getKey();

                if(!userUid.equals(mCurrentUserUid)) {

                    //Get recipient user name
                    UsersChatModel user = dataSnapshot.getValue(UsersChatModel.class);

                    //Add recipient uid
                    user.setRecipientUid(userUid);

                    //Add current user (or sender) info
                    user.setCurrentUserEmail(mCurrentUserEmail); //email
                    user.setCurrentUserUid(mCurrentUserUid);//uid
                    mUsersKeyList.add(userUid);
                    mUsersChatAdapter.refill(user);

                }else{
                    UsersChatModel currentUser = dataSnapshot.getValue(UsersChatModel.class);
                    String userName=currentUser.getFirstName(); //Get current user first name
                    String createdAt=currentUser.getCreatedAt(); //Get current user date creation
                    mUsersChatAdapter.setNameAndCreatedAt(userName, createdAt); //Add it the adapter
                }
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            if(dataSnapshot.exists()) {
                String userUid = dataSnapshot.getKey();
                if(!userUid.equals(mCurrentUserUid)) {
                    UsersChatModel user = dataSnapshot.getValue(UsersChatModel.class);

                    // Removed bug here
                    //Add recipient uid
                    user.setRecipientUid(userUid);

                    //Add current user (or sender) info
                    user.setCurrentUserEmail(mCurrentUserEmail); //email
                    user.setCurrentUserUid(mCurrentUserUid);//uid
                    int index = mUsersKeyList.indexOf(userUid);
                    Log.e(TAG, "change index "+index);
                    mUsersChatAdapter.changeUser(index, user);
                }

            }


        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

并且

    mFireChatUsersRef=new Firebase(ReferenceUrl.FIREBASE_CHAT_URL).child(ReferenceUrl.CHILD_USERS);

0 个答案:

没有答案