检查Firebase

时间:2017-01-14 09:42:41

标签: android firebase firebase-realtime-database

这是我的客户节点

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"
 }
}

我使用以下查询来查看电话号码

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

如何检查值" 789"存在?

尝试做

 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) {

        }

这不起作用。可以选择检查吗?

1 个答案:

答案 0 :(得分:1)

您应该使用addListenerForSingleValueEvent

仅当子项存在时才会调用ChildEventListener。如果不是,就不会发生任何事情。

始终会调用ValueEventListener,您可以调用快照的exists()方法来检查是否存在。