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

我试图通过在 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);

            }


        }

    });

}

拉丁的传说
浏览 115回答 1
1回答

萧十郎

如果数据库中不存在用户名,则使 checkifUsernameExists() 方法无效并在其中调用 registerUser() 方法。mCreateBtn.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; String username = musername.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; String email = mEmail.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; String password = mPassword.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; if (TextUtils.isEmpty(username) || TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(registerActivity.this, "Cannot sign you in. Please check the form and try again",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; } else if (password.length() < 6) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(registerActivity.this, "Password must be at least 6 characters",Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; } else if (email_exists == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(registerActivity.this, "Email already exists",Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkifUsernameExists(username, email, password);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }});private void checkifUsernameExists(final String username, final String email, final String password) {&nbsp; &nbsp; Query usernameQuery = FirebaseDatabase.getInstance().getReference().child("users").orderByChild("username").equalTo(username);&nbsp; &nbsp; usernameQuery.addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dataSnapshot.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the username is already in the database&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(registerActivity.this, "Username already exists", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.setTitle("Registering user");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.setMessage("Pleases wait while we create your account");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.setCanceledOnTouchOutside(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; registerUser(username, email, password);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java