为了账号安全,请及时绑定邮箱和手机立即绑定

检查用户名是否存在于 firebase 数据库中

检查用户名是否存在于 firebase 数据库中

拉丁的传说 2022-12-21 10:57:56
我试图通过在 if 语句中调用函数 checkifUsernameExists(username) 来检查数据库中是否存在用户名。问题是 firebase 的内置函数称为 public void onDataChange(@NonNull DataSnapshot dataSnapshot)不能返回布尔值。我能做什么?    mCreateBtn.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            String username = musername.getText().toString();            String email = mEmail.getText().toString();            String password = mPassword.getText().toString();            if (TextUtils.isEmpty(username) || TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {                Toast.makeText(registerActivity.this, "Cannot sign you in. Please check the form and try again",                        Toast.LENGTH_SHORT).show();            } else if (password.length() < 6) {                Toast.makeText(registerActivity.this, "Password must be at least 6 characters",Toast.LENGTH_LONG).show();            } else if (email_exists == true) {                Toast.makeText(registerActivity.this, "Email already exists",Toast.LENGTH_LONG).show();            } else if (checkifUsernameExists(username)) {                Toast.makeText(registerActivity.this, "Username already exists", Toast.LENGTH_LONG).show();            } else {                message.setTitle("Registering user");                message.setMessage("Pleases wait while we create your account");                message.setCanceledOnTouchOutside(false);                message.show();                //registerUser(username, email, password);            }        }    });}
查看完整描述

1 回答

?
萧十郎

TA贡献1815条经验 获得超13个赞

如果数据库中不存在用户名,则使 checkifUsernameExists() 方法无效并在其中调用 registerUser() 方法。


mCreateBtn.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View view) {


        String username = musername.getText().toString();

        String email = mEmail.getText().toString();

        String password = mPassword.getText().toString();


        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {

            Toast.makeText(registerActivity.this, "Cannot sign you in. Please check the form and try again",

                    Toast.LENGTH_SHORT).show();

        } else if (password.length() < 6) {

            Toast.makeText(registerActivity.this, "Password must be at least 6 characters",Toast.LENGTH_LONG).show();

        } else if (email_exists == true) {

            Toast.makeText(registerActivity.this, "Email already exists",Toast.LENGTH_LONG).show();

        } else {

            checkifUsernameExists(username, email, password);

        }


    }

});


private void checkifUsernameExists(final String username, final String email, final String password) {


    Query usernameQuery = FirebaseDatabase.getInstance().getReference().child("users").orderByChild("username").equalTo(username);

    usernameQuery.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override

        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {

                // the username is already in the database

                Toast.makeText(registerActivity.this, "Username already exists", Toast.LENGTH_LONG).show();

            }else {

                message.setTitle("Registering user");

                message.setMessage("Pleases wait while we create your account");

                message.setCanceledOnTouchOutside(false);

                message.show();

                registerUser(username, email, password);

            }

        }


        @Override

        public void onCancelled(@NonNull DatabaseError databaseError) {


        }

    });

}


查看完整回答
反对 回复 2022-12-21
  • 1 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信