繁体   English   中英

如何在视图中显示文档的所有字段?

[英]How to show all fields of a document in view?

我必须在我的 Android 应用程序的 Firebase Firestore 中列出文档中存在的所有字段。 如何实施?

例如,在随附的屏幕截图中,我必须在我的应用程序中显示文档“hIYsLb7YW0dGe5b1mmpiGrEbYJ03”中的所有字段。

文件截图

为了能够显示“hIYsLb7YW0dGe5b1mmpiGrEbYJ03”中所有字段的名称,请使用以下代码行:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference docId = rootRef.collection("UsersLogDetail").document("hIYsLb7YW0dGe5b1mmpiGrEbYJ03");
docId.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            List<String> fieldNames = new ArrayList<>();
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Map<String, Object> map = document.getData();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    fieldNames.add(entry.getKey());
                }
                Log.d(TAG, fieldNames.toString());
                //Do what you need to do with your services List of property names
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException()); //Don't ignore potential errors!
        }
    }
});

如您所见,您需要阅读整个文档的内容。 请注意,DocumentSnapshot 的getData()方法返回一个 Map<String, Object>,它可以像 Java 中的任何其他 Map 一样简单地迭代。

logcat 中的结果将是:

[07-04-21 17:45:26, 07-04-21 17:45:46 ... 07-04-21 17:46:55]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM