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

时间:2019-05-29 16:01:06

标签: java firebase firebase-realtime-database

试图通过调用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);
            }

        }
    });
}

    private boolean checkifUsernameExists(String username) {
        Query usernameQuery = FirebaseDatabase.getInstance().getReference().child("users").orderByChild("username").equalTo(username);
        usernameQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.getChildrenCount() > 0) {

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

如果用户名不存在于数据库中,则使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) {

        }
    });
}