【问题标题】:Get all fields in a document in a list - Firestore Java获取列表中文档中的所有字段 - Firestore Java
【发布时间】:2018-08-09 11:38:20
【问题描述】:

我正在尝试将文档中的所有字段获取到ListView。我尝试过 foreach 循环,但它不起作用。

dbRef.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com")
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            // Get all fields to a list
        }
    });

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    要将文档中所有属性的所有值添加到列表中,请使用以下代码行:

    dbRef.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    List<String> list = new ArrayList<>();
    
                    Map<String, Object> map = document.getData();
                    if (map != null) {
                        for (Map.Entry<String, Object> entry : map.entrySet()) {
                            list.add(entry.getValue().toString());
                        }
                    }
    
                    //So what you need to do with your list
                    for (String s : list) {
                        Log.d("TAG", s);
                    }
                }
            }
        }
    });
    

    【讨论】:

    • 这正是我想要做的。谢谢
    • 这正是我在过去一小时中发现的。非常感谢您的帮助。我试图在 recyclerview 中显示文档的所有字段,这段代码对我真的很有帮助。我用它来获取键和值,我将此代码用作entry.getKey()entry.getValue()。再次感谢。
    • @Pooja 很高兴听到这个消息;)
    【解决方案2】:

    要获取字段,请使用以下命令:

    DocumentReference docRef = db.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
      @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                Log.d(TAG,"String value: " + document.getString("names"));
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
      }
    });
    

    public Map&lt;String, Object&gt; getData ()

    如果文档不存在,则将文档的字段作为 Map 或 null 返回。字段值将被转换为其本机 Java 表示形式。

    或者你可以使用getString()

    【讨论】:

      【解决方案3】:

      试试这个

           List<Type> types = documentSnapshots.toObjects(Type.class);   
      

      你的例子会是这样的

      dbRef.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com")
                  .get()
                  .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
              @Override
              public void onSuccess(DocumentSnapshot documentSnapshot) {
                  if(documentSnapshots.isEmpty()){
                    Log.d("MyLogger", "Empty Document!");
                  }else{
                  // Get all fields to a list
                   List<MyModel> types = documentSnapshots.toObjects(MyModel.class); 
                 }  
              }
          });
      
      public class MyModel{
        // Define fields
        private String id;
        private String name; // ...etc
      
        // GETTER/SETTER, 
      }
      

      【讨论】:

        【解决方案4】:

        试试这个,

        确保您在manifest 中拥有互联网权限, 并且您的项目已连接到 firebase。

        private static final String KEY_NAME = "name";
        public void loadName() {
        
            db.collection("users").document()
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                            if (task.isSuccessful()) {
                                DocumentSnapshot document =  task.getResult();
                                String name = document.getString(KEY_NAME);
        
                            } else {
                                Toast.makeText(activity_home.this, "Error", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
        }
        

        【讨论】:

          猜你喜欢
          • 2021-10-09
          • 1970-01-01
          • 1970-01-01
          • 2022-12-13
          • 2019-12-29
          • 1970-01-01
          • 2022-10-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多