尝试根据当前的userUID

时间:2018-08-20 14:45:14

标签: java android firebase google-cloud-firestore

我正在尝试检索当前登录的学生的姓名。为此,我正在尝试检查当前用户UID,如果它等于学生集合中的userUID,请返回学生姓名作为字符串。这是我查看所有学生的代码。我想将其作为功能而不是查询。

public void viewStudents(View v){
    studentRef2.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            String data = "";

            for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
                Student student = documentSnapshot.toObject(Student.class);
                //student.setDocumentID(documentSnapshot.getId());

                String course = student.getCourse();
                String name = student.getName();
                String studentID = student.getStudentID();
                String userUID = student.getUserUID();
                //String documentID = student.getDocumentID();

                data +=  "Name: " +name+ "\n" + "Course: " +course+ "\n" +"Student ID: " +studentID+
                        "\n" +"User UID: " +userUID+ "\n\n";
                }
                textViewData.setText(data);
        }
    });

1 个答案:

答案 0 :(得分:0)

要解决此问题,您应该实现如下的if语句:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
String userUID = student.getUserUID();
if(uid.equals(userUID)) {
    textViewData.setText(name);
}

因此,首先获得当前登录用户的uid,然后使用String.equals()方法将其与来自数据库的用户的uid进行比较,如果匹配,则您将名称分配给您的textViewData

还有另一种方法,您可以像这样从name对象中获取用户FirebaseUser,而无需遍历数据库:

String name = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
textViewData.setText(name);