如何检查firebase数据库中是否存在值 - android [英] How to check if a value exists in firebase database - android

查看:144
本文介绍了如何检查firebase数据库中是否存在值 - android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android(Java)的firebase新手,想知道如何检查用户是否已存在于以下格式的数据库中:

I am new to firebase via Android(Java) and was wondering how to check if a user already exists in a database of the following format:

    gdb-2fbgg{

      users{


         Michael{
              age: 20
              height: 150
              name: Michael
                }


         Bob{
              age: 20
              height: 150
              name: Bob
                }
       }
}

例如,按下按钮,我想查看firebase以查看用户名Michael是否存在(第一级michael - 而不是对象michael中的michael。

For example upon a button press, I would like to check the firebase to see if the username "Michael" exists ( first level michael - not "michael" within object michael.

谢谢

推荐答案

使用 .child(Michael)。exists()方法或 .hasChild(Michael)在onDataChange中使用SingleValueEvent

Use .child("Michael").exists() method or .hasChild("Michael") with SingleValueEvent within onDataChange

DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference users = root.child("users");
users.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            if (snapshot.child("Michael").exists()) {
                // run some code
            }else{

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

这篇关于如何检查firebase数据库中是否存在值 - android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆