0

This is my Customer's Node

customers
 {
 "-KXKCee0yVJE1z1gCQcV" : {
"cust_id" : "-KXKCee0yVJE1z1gCQcV",
"email" : “xxx@xxx.com",
"food_pref" : "nonveg",
"name" : “Jind",
"notes" : "",
"phone" : “123"
 },
 "-KXMVChcSwfUif-xVbRa" : {
"cust_id" : "-KXMVChcSwfUif-xVbRa",
"email" : “ggg@ggg.com",
"food_pref" : "nonveg",
"name" : “Mani",
"notes" : "Butter Chicken!!!",
"phone" : “456"
  },
  "-KXV4eYM6bKNniE_oLEf" : {
"cust_id" : "-KXV4eYM6bKNniE_oLEf",
"email" : “vvv@vvv.com",
"food_pref" : "veg",
"name" : "Jay",
"notes" : "",
"phone" : “789"
 }
}

I'm using the following query to check Phone Number

Query query = reference.child("customers").orderByChild("phone").equalTo("789");

How do I check if the value "789" exists?

Tried doing

 query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.exists())
                Log.d(TAG, dataSnapshot.getKey());
            else
                Log.d(TAG, "Not Found");
        }

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

        }

This does not work.Any alternate for checking this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36

1 Answers1

1

You should use addListenerForSingleValueEvent.

The ChildEventListener will be invoked only if the child exists. If it doesn't, nothing will happen.

The ValueEventListener will always be invoked and you can call the snapshot's exists() method to check for existence.

cartant
  • 57,105
  • 17
  • 163
  • 197