【发布时间】:2020-03-23 01:23:09
【问题描述】:
我似乎无法将文本放入 TextView。数据来自 Firestore 数据库。当我运行它时,TextView 是空白的并返回一个空值。以下是我到目前为止所做的。有人可以看看吗?你可以在这里看到我的数据库结构:[数据库结构图][1]
public class ProfileFragment extends Fragment {
TextView firstNameText, age;
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
firstNameText = rootView.findViewById(R.id.profile_firstName);
age = rootView.findViewById(R.id.profile_age);
getUserInfo();
return rootView;
}
public void getUserInfo() {
db.collection("Users").document(mAuth.getCurrentUser().getEmail()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
String firstName = documentSnapshot.getString("Firstname");
Log.d(TAG, "Printing docData: " + documentSnapshot.getData());
firstNameText.setText(firstName);
}
}
});
}
}
【问题讨论】:
-
请将您的数据库结构添加为 JSON 文件或至少是屏幕截图。
-
@AlexMamo 谢谢,我已经添加了图片。
-
您的代码是否包含在成功响应中?还是失败?什么日志显示?响应的任务对象是什么?
-
在使用这行代码
Log.d(TAG, "Printing docData: " + documentSnapshot.getData());时,是不是在logcat中打印了一些东西?也请回复@AlexMamo -
你能分享一下日志吗?以及documentSnapshot的内容
标签: java android firebase android-fragments google-cloud-firestore