繁体   English   中英

如何从 Firebase Firestore 的地图字段中获取数据?

[英]How to get data from a map field from Firebase Firestore?

我想检索存储为 Cloud Firestore 上的地图字段的数据。
我想从“所有评论”字段中获取“评论”作为字符串,以在 TextView 中显示它。

我该怎么做? (爪哇) 在此处输入图像描述

我试过这个来添加数据

                Map<String,String> allComments=new HashMap<String,String>();
                String commentContent=commentboxedittext.getText().toString();
                allComments.put("Movie Name",name);
                allComments.put("Comment",commentContent);
                firebaseFirestore.collection("All Comments").document("MovieComments").set(allComments, SetOptions.merge());  

这是检索数据

DocumentReference docRef = firebaseFirestore.collection("All Comments").document("MovieComments");
                docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            if (document.exists()) {
                                Map<String, Object> m=document.getData();
                                userComment=m.get("Comment").toString();
                                mName=m.get("Movie Name").toString();
                            } else {
                                Toast.makeText(MovieDetails.this, "No Such Document", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(MovieDetails.this, "Error", Toast.LENGTH_SHORT).show();
                        }
                    }
                });  

但是应用程序在执行此操作时崩溃。
我也尝试这样做来放置数据并且它有效,但是我不知道如何从这种方法中检索数据。

            Map<String,String> allComments=new HashMap<String,String>();
            Map<String, Object> user=new HashMap<String,Object>();
        userID=firebaseAuth.getCurrentUser().getUid();
        userReference=firebaseFirestore.collection("Users ").document(userID);
            String commentContent=commentboxedittext.getText().toString();
            allComments.put("Movie Name",name);
            allComments.put("Comment",commentContent);
            user.put("All Comments",allComments);
            userReference.set(user, SetOptions.merge()).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void unused) {
                    Toast.makeText(MovieDetails.this, "Comment Added", Toast.LENGTH_SHORT).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if(e instanceof FirebaseNetworkException)
                        Toast.makeText(MovieDetails.this, "No Internet Connection", Toast.LENGTH_SHORT).show();
                    Toast.makeText(MovieDetails.this, "Values Not Stored", Toast.LENGTH_SHORT).show();
                }
            });

假设“l4ir...Xy12”是经过身份验证的用户的 ID,要获取“所有评论”映射中存在的“评论”的值,请使用以下代码行:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users").document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                String comment = ((HashMap<String, Object>) document.getData().get("All Comments")).get("Comment").toString();
                Log.d("TAG", comment);
            } else {
                Log.d("TAG", "No such document");
            }
        } else {
            Log.d("TAG", "get failed with ", task.getException());
        }
    }
});

logcat 中的结果将是:

sfgs

需要注意的几点:

  • DocumentSnapshot#get(String field)返回一个 Object 类型的 对象 由于文档中的每个字段都代表一对键和值,因此我们可以将结果转换为HashMap<String, Object>类型的对象。
  • 由于我们已经有了一个 Map,我们可以调用Map#get(Object key)方法,该方法返回与该键关联的值。

暂无
暂无

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

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