Firebase检查节点A中的子值是否与节点B中的子值匹配

时间:2017-05-02 15:58:02

标签: android firebase firebase-realtime-database

我的Firebase数据库中有以下数据结构,我正在尝试搜索'用户'节点由电话号码'当我在“玩家”中添加新玩家时,看看是否有任何匹配。节点

我认为我正在侦听错误的节点或者错误地使用addChildEventListener,因为当我添加一个具有相同数字的播放器时它会返回else语句' NOT FOUND '

任何帮助非常感谢

Image of datastructure

以下代码

    usersDatabase = mFirebaseDatabase.getReference().child("users");
private void checkIfPlayerIsUser(final String phoneNum){
  //  String ph = phoneNum.substring(phoneNum.length() - 8);
    usersDatabase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.getValue() == null) {
                Toast.makeText(getApplicationContext(), "****NULLL****", Toast.LENGTH_LONG).show();

            }
            else if(dataSnapshot.getValue().equals(phoneNum)){

                // player added is in user database
                Toast.makeText(getApplicationContext(), "****PLAYER FOUND****", Toast.LENGTH_LONG).show();

            }
            else{
                Toast.makeText(getApplicationContext(), "****NOT FOUND****", Toast.LENGTH_LONG).show();

            }
        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

2 个答案:

答案 0 :(得分:1)

您可以使用以下方法。我在运行应用程序时使用它来检查用户名是否被采用。我希望它有所帮助。

  void checkIfPlayerIsUser(final String phoneNum) {
    DatabaseReference mDatabaseReference = 
         FirebaseDatabase.getInstance().getReference().child("users");

    if (!TextUtils.isEmpty(phoneNum)) {
        final Query phoneNumReference = mDatabaseReference.orderByChild("phoneNum").equalTo(phoneNum);

        ValueEventListener phoneNumValueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.getValue() != null) {
                        Toast.makeText(getApplicationContext(), "****PLAYER FOUND****", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "****NOT FOUND****", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {}
            };

            phoneNumReference.addListenerForSingleValueEvent(phoneNumValueEventListener);


    } else {
         Log.e("Error","phoneNum is null");
    }
}

答案 1 :(得分:0)

您的DatabaseReference错了。为了获得正确的phoneNum,您需要使用此代码:

usersDatabase = mFirebaseDatabase.getReference().child("users").child(userId);

其中userIdpush()方法生成的唯一键。

请检查,请使用:

DatabaseReference usersDatabase = mFirebaseDatabase.getReference().child("users");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String userKey = ds.getKey();

            DatabaseReference userKeyDatabase =  mFirebaseDatabase.getReference().child("users").child(userKey);
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot == null) {
                        Toast.makeText(getApplicationContext(), "****NULLL****", Toast.LENGTH_LONG).show();

                    } else if(dataSnapshot.child("phoneNum").getValue().equals(phoneNum)){
                        // player added is in user database
                        Toast.makeText(getApplicationContext(), "****PLAYER FOUND****", Toast.LENGTH_LONG).show();

                    } else {
                        Toast.makeText(getApplicationContext(), "****NOT FOUND****", Toast.LENGTH_LONG).show();

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {}
            };
            userKeyDatabase.addListenerForSingleValueEvent(eventListener);

        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersDatabase.addListenerForSingleValueEvent(valueEventListener);

希望它有所帮助。