Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查用户名是否已从中获取_Java_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Java 如何检查用户名是否已从中获取

Java 如何检查用户名是否已从中获取,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,看,我有我的firestore数据库,我想检查我的一个用户文档是否已经有用户在textview中键入的用户名 usersRef = db.collection("users"); userNameTextView = findViewById(R.id.setup_username); String username = userNameTextView.getText().toString(); Query query = usersRef.orderBy("username").wh

看,我有我的firestore数据库,我想检查我的一个用户文档是否已经有用户在textview中键入的用户名

 usersRef = db.collection("users");
 userNameTextView = findViewById(R.id.setup_username);
 String username = userNameTextView.getText().toString();
 Query query = usersRef.orderBy("username").whereEqualTo("username", userNameTextView.getText().toString());
if(query.get().isSuccessful()) {
            Toast.makeText( this, "Username is taken...", Toast.LENGTH_SHORT ).show();
        }
这是数据库结构,请注意:


但不知怎的,它不起作用了,有人有主意了吗?

你只是在检查任务是否成功,这应该只意味着你成功地与服务器进行了沟通。您还应该检查结果的大小,以确定是否找到任何匹配的文档

所有这些都应该在侦听器回调中发生,因为您正在等待服务器的响应:

db.collection("myCollection")
    .whereEqualTo("someField", "value")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { // Add the listener callback
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            // Check if our task successfully communicated with the server
            if(task.isSuccessful()) {

                // Check the size of the result to see if any matches were found
                if(task.getResult().size() == 0){
                    // No document exists containing the searched value
                }else{
                    // A document already exists containing the searched value
                }

            }else{
                Log.e("MyLogTag", "There was an error querying the documents.", task.getException());
            }
        }
    });
db.collection(“myCollection”)
.whereEqualTo(“某个字段”、“值”)
.get()
.addOnCompleteListener(新的OnCompleteListener(){//添加侦听器回调
@凌驾
未完成的公共void(@NonNull任务){
//检查我们的任务是否与服务器成功通信
if(task.issusccessful()){
//检查结果的大小以查看是否找到任何匹配项
if(task.getResult().size()==0){
//不存在包含搜索值的文档
}否则{
//包含搜索值的文档已存在
}
}否则{
Log.e(“MyLogTag”,“查询文档时出错。”,task.getException());
}
}
});

因为我们看不到您查询的数据,所以我们无法判断您是否做错了什么。请使用具体示例编辑问题,以明确您希望此查询返回的内容。我建议对所有字符串进行硬编码,并显示与您的查询匹配的文档的屏幕截图。我添加了数据库屏幕截图现在,编辑您的代码以硬编码查询参数。我们不知道
userNameTextView.getText().toString()
返回什么。它可能不是您所期望的。它是一个文本视图,所以它返回一个字符串?您可以告诉我们。我们看不到您的文本视图中有什么内容。正如我所说,您应该对查询中的值进行硬编码,以帮助我们知道您是否真的在查询您期望的内容。还要考虑在把所有相关的值传递给一个API之前记录所有的值,这样你就可以确定发生了什么。