java - 从 Firestore 获取 ArrayList 和文档名称
问题描述
我是 Google Firebase 的新手,我正在尝试了解它。我正在做一个 Android 应用程序,您可以在其中创建一组人员并设置组的标题。然后,在“组页面”中,您可以在列表视图中看到您的所有组。我的 Firestore 数据库的结构是这样的:
users --> email(document) ---> Group(collection) --> GroupName(Document)并且组名文档包含参与者数组列表(参与者 0:Name1,partecipant1:name2 等)。
我想检索文档ID(这是组标题)和参与者的arrayList,但我不知道在代码中使用for each ...
这是我的代码:
public void load_list_view(){
String email = getEmail();
final DocumentReference docRef = db.collection("users").document(email).collection("Group").document();
docRef.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
titleArray.add(documentSnapshot.getId());
titleString = documentSnapshot.getId();
partecipantsArray.add(documentSnapshot.getString("partecipant"));
num_partecipants = partecipantsArray.size();
numArray.add(num_partecipants);
trash = R.drawable.trash_icon;
firstChar = Character.toString(titleString.charAt(0));
firstCharArray.add(firstChar);
customAdapter = new GroupAdapter(GroupActivity.this, firstCharArray, titleArray, numArray, trash);
listView.setAdapter(customAdapter);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(GroupActivity.this, e.getStackTrace().toString(), Toast.LENGTH_LONG).show();
}
});
}
用titleArray.add(documentSnapshot.getId());
它检索一个随机 ID,我不明白为什么。
我在 Internet 上没有找到足够的关于Arraylist
和 firestore 的文档。
解决方案
首先,要获取集合中的所有文档,您应该按照本文档中所示的方式编写不同的代码。
db.collection("cities")
.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());
}
}
});
其次,如果您正在检索一个 ArrayList,您应该使用(ArrayList<String>) documentSnapshot.get("key")
而不是documentSnapshot.getString("key")
.
第三,您将获得随机 ID,因为使用这行代码(如下所述),firebase 正在生成一个带有随机 ID 的新文档引用。参考链接。
final DocumentReference docRef = db.collection("users").document(email).collection("Group").document();
为了您的帮助,我已经调整了您的代码,您可以尝试此代码并检查它是否正常工作。
public void load_list_view() {
String email = getEmail();
final DocumentReference docRef = firestore.collection("users").document(email);
docRef.collection("Group")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
//Extracting Group name from each document
titleString = document.getId();
titleArray.add(titleString);
//Extracting participants ArrayList from each document
partecipantsArray.add((ArrayList<String>) document.get("participant"));
numArray.add(num_partecipants);
firstChar = Character.toString(titleString.charAt(0));
firstCharArray.add(firstChar);
}
num_partecipants = partecipantsArray.size();
numArray.add(num_partecipants);
trash = R.drawable.trash_icon;
firstChar = Character.toString(titleString.charAt(0));
firstCharArray.add(firstChar);
customAdapter = new GroupAdapter(GroupActivity.this, firstCharArray, titleArray, numArray, trash);
listView.setAdapter(customAdapter);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//HANDLE EXCEPTION
}
});
}
推荐阅读
- android - 使用 minifyEnabled true 启动应用程序时 FirebaseInitProvider.onCreate 中的 java.lang.IncompatibleClassChangeError
- firebase - Flutter:如何计算 Firebase 字段中的项目?
- python - 在 Visual Studio 代码中使用 python 处理两个 32GB 文件,几天后未处理
- flutter - 亲爱的所有颤振开发者我想问一下“颤振概念”下面的图像如何画线?
- javascript - 在不重新加载页面的情况下更改脚本内容
- python - Numpy 检查每个数据类型的值并返回布尔值
- javascript - 我该怎么做才能通过悬停和单击来使此 javascript 代码工作?
- java - 为什么我的 while 循环会在 Java 中产生额外的值?
- python - 通过 Python 从其他文件中搜索其值后更新特定 JSON 文件参数
- gnuplot - 如何删除gnuplot中的所有零点?