繁体   English   中英

在列表中获取文档中的所有字段-Firestore Java

[英]Get all fields in a document in a list - Firestore Java

我试图从文档到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
        }
    });

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

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<String, Object> getData ()

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

或者您可以使用getString()

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

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);
                }
            }
        }
    }
});

尝试这个

     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, 
}

尝试这个,

确保manifest具有Internet权限 ,并且您的项目已连接到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();
                    }
                }
            });
}

暂无
暂无

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

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