Java 如何从 Firestore Android 中的集合中获取文档列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50035752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get list of documents from a collection in Firestore Android
提问by Ivan Banha
My structure of Firestore database:
我的 Firestore 数据库结构:
|
|=>root_collection
|
|=>doc1
|
|=>collection
|
|=>doc2
|
|=>collection
|
|=>doc3
|
|=>collection
Now I wanna get list of document from root_collection
. There would be a list with following data {"doc1", "doc2", "doc3"}
. I need it because I want to make a spinner and put these data in the spinner. Then a user would be choose some document and download it.
现在我想从root_collection
. 会有一个包含以下数据的列表{"doc1", "doc2", "doc3"}
。我需要它,因为我想制作一个微调器并将这些数据放入微调器中。然后用户将选择一些文件并下载它。
I try to use the code below:
我尝试使用下面的代码:
firestore.collection("root_collection")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG,document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
But the code works only then I have structure of data without collections in the documents. In other case there aren't any documents in QueryDocumentSnapshot
.
但是代码只有在我拥有文档中没有集合的数据结构时才有效。在其他情况下,QueryDocumentSnapshot
.
Thanks!
谢谢!
采纳答案by Alex Mamo
To have a list that contains all the name of your documents within the root_collection
, please use the following code:
要获得包含 中所有文档名称的列表root_collection
,请使用以下代码:
firestore.collection("root_collection").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getId());
}
Log.d(TAG, list.toString());
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
The result in your logcat will be:
您的 logcat 中的结果将是:
[doc1, doc2, doc3]
Remember, this code will work, only if you'll have some properties within those documents, otherwise you'll end ut with an empty list.
请记住,此代码将起作用,仅当您在这些文档中具有某些属性时,否则您将以空列表结束 ut。
回答by Techno Peace
@Alex Mamo's reply does works. But there is a condition. If you use Auth in your Firebase Project, you cant reach data in your database if you've not logged in with Firebase Auth. It is actually good for us cause no one who has been not logged in cant reach our database. It is safe.
@Alex Mamo 的回复确实有效。但是有一个条件。如果您在 Firebase 项目中使用 Auth,并且未使用 Firebase Auth 登录,则无法访问数据库中的数据。这实际上对我们有好处,因为没有登录的人无法访问我们的数据库。这是安全的。
回答by Mihango K
You can call collection method to get documentin the root_collections then hold documents ID which will be used to get document's collection later.
您可以调用 collection 方法获取 root_collections 中的文档,然后保存文档 ID,稍后将用于获取文档的集合。
create root collection object as:
创建根集合对象为:
data class RootCollection(
@DocumentId val id: String,
val sampleField: String?
) {
// empty constructor to allow deserialization
constructor(): this(null, null)
}
Then get the collection using FirebaseFirestore method collection as follows:
然后使用 FirebaseFirestore 方法集合获取集合,如下所示:
val querySnapshot = firestore.collection("root_collection")
.get()
.await()
if(!querySnapshot.isEmpty) {
Result.SUCCESS(querySnapshot.toObjects(RootCollection::class.java))
} else {
Result.ERROR(Exception("No data available"))
}
Then to get collection in the document call
然后在文档调用中获取集合
firestore.collection("root_collection/$documentId/collection")
Note that I have use kotlin coroutines await method as describe in this link
请注意,我使用了此链接中描述的 kotlin 协程等待方法
The Result class am using to save state of returned data and handle errors.
Result 类用于保存返回数据的状态并处理错误。
sealed class Result<out T: Any> {
data class SUCCESS<out T: Any>(val data: T) : Result<T>()
data class ERROR(val e: Exception): Result<Nothing>()
object LOADING : Result<Nothing>()
}