how to know if firebase ChildEventListener found child ? [android]

前端 未结 1 707
名媛妹妹
名媛妹妹 2020-12-06 15:14

so I\'m trying to get users key by their email and the problem is that I dont know inside my code if the query actually found something or not .. so I\'m assuming if I\'m in

相关标签:
1条回答
  • 2020-12-06 16:00

    The methods of a ChildEventListener get called when the relevant event happened. So onChildAdded() will be called when a child has been added. For this reason you cannot easily use a ChildEventListener to detect if a child exists.

    The easiest way to detect if a child exists, is to use a ValueEventListener:

    public void searchemail(String email){
    
      Firebase ref = new Firebase("https://<myfirebase>.firebaseio.com/users");
      Query queryRef = ref.orderByChild("Email").equalTo(email);
    
    
       ValueEventListener listener = new ValueEventListener() {
    
         @Override
         public void onDataChanged(DataSnapshot snapshot) {
           if (snapshot.exists()) {
             for (DataSnapshot child: snapshot.getChildren()) {
               homeintent.putExtra("key", child.getKey());
               startActivity(homeintent);
               break; // exit for loop, we only want one match
             }
           }
           else {
             Toast toast = Toast.makeText(this, "email not found", Toast.LENGTH_SHORT);
           }
         }
       };
       queryRef.addValueEventListener(listener);
    }
    
    0 讨论(0)
提交回复
热议问题