[英]Firebase, get an arraylist field from all documents
I need to query a collection..this collection "Group" contains many documents, and each of them contains, obviously, a title(the id), an array of partecipants, with field named "partecipant", and a field "numPartecipants",with the total partecipants of each group. 我需要查询一个集合。该集合“组”包含许多文档,并且每个文档都包含一个标题(id),一组参与者数组,其字段名为“ partecipant”,以及一个字段“ numPartecipants” ,以及每组的总参加人数。 I attach a photo of the db:
我附上一张分贝的照片:
Now, I can get the "partecipant" field and save it into an array with this code: 现在,我可以使用以下代码获取“ partecipant”字段并将其保存到数组中:
public void getPartecipantsList(){
String email = getEmail();
final String groupTitle = getTitleBar();
DocumentReference docRef = db.collection("users").document(email).collection("Group").document(groupTitle);
docRef.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot document = task.getResult();
//Extracting participants ArrayList from the document
for(Object item : task.getResult().getData().values()) {
String[] values = String.valueOf(item).replace("[", "").replace("]", "").replace(" ", "").split(",");
for (String value : values){
partecipantsArrayList.add(value);
}
}
partecipantsArrayList.remove(String.valueOf("["));
partecipantsArrayList.remove(partecipantsArrayList.size() - 1);
But that's work if I know the name of the group. 但是,如果我知道小组的名称,那是可行的。 In this case, I want to extract from the "Group" collection each documents, and from each documents the Id and the partecipants array..I think I can't use Task, but I have to use QueryDocumentSnapshots.
在这种情况下,我想从“组”集合中提取每个文档,并从每个文档中提取Id和partecipants数组。.我认为我不能使用Task,但是必须使用QueryDocumentSnapshots。 That's my code.
那是我的代码。
public void getPartecipantsList() {
final ArrayList<String> title = new ArrayList<>();
String email = getEmail();
DocumentReference docRef = db.collection("users").document(email);
docRef.collection("Group")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
titleString = document.getId();
title.add(titleString);
String[] values = String.valueOf(document).replace("[", "").replace("]", "").replace(" ", "").split(",");
for (String value : values) {
partecipantsArray.add(value);
}
}
partecipantsArray.remove(String.valueOf("["));
partecipantsArray.remove(partecipantsArray.size() - 1);
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
for (int i = 0; i < title.size(); i++) {
editor.putInt(title.get(i) + "_size", partecipantsArray.size());
}
for (int i=0;i<title.size();i++) {
Log.v("Array", partecipantsArray.toString());
Log.v(title.get(i)+"array", String.valueOf(partecipantsArray.size()));
}
for (int i = 0; i < partecipantsArray.size(); i++) {
editor.putString(titleString + "_name" + i, partecipantsArray.get(i)); }
}
}
});
}
And the output is completely wrong: 输出完全错误:
The output instead should be: title = [New York, Prova, Test] partecipantsArray = [Andrea, Tom, Spencer ........ , Nico, Raul, Lorenzo, Eli] 输出应该改为:title = [New York,Prova,Test] partecipantsArray = [Andrea,Tom,Spencer ........,Nico,Raul,Lorenzo,Eli]
To get the array from within a single document, only these lines of code are required: 要从单个文档中获取数组,只需要以下几行代码:
db.collection("users").document(email).collection("Group").document(groupTitle).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
ArrayList<String> arrayList = (ArrayList<String>) document.get("partecipant");
//Do what you need to do with your ArrayList
for (String s : arrayList) {
Log.d(TAG, s);
}
}
}
}
});
The output in your logcat will be: 您的logcat中的输出将是:
Nico
Raul
Lorenzo
Eli
If you want to get all arrays from within all documents, please use the following lines of code: 如果要从所有文档中获取所有数组,请使用以下代码行:
db.collection("users").document(email).collection("Group").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document.exists()) {
ArrayList<String> arrayList = (ArrayList<String>) document.get("partecipant");
//Do what you need to do with your ArrayList
for (String s : arrayList) {
Log.d(TAG, s);
}
}
}
}
}
});
The output will be all partecipants within all documents. 输出将是所有文档中的所有参与者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.