我需要查询一个集合..这个集合“Group”包含许多文档,显然每个文档都包含一个标题(id)、一个参与者数组(其中字段名为“partecipant”)和一个字段“numPartecipants”,每组的参与者总数。我附上数据库的照片:
现在,我可以使用以下代码获取“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);
但是如果我知道该组的名称就可以了。 在本例中,我想从“Group”集合中提取每个文档,并从每个文档中提取 Id 和参与者数组。我想我不能使用 Task,但我必须使用 QueryDocumentSnapshots。 这是我的代码。
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)); }
}
}
});
}
输出应该是: title = [纽约、普罗瓦、测试] partecipantsArray = [Andrea、Tom、Spencer ........、Nico、Raul、Lorenzo、Eli]
最佳答案
要从单个文档中获取数组,只需要以下代码行:
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);
}
}
}
}
});
logcat 中的输出将是:
Nico
Raul
Lorenzo
Eli
如果您想从所有文档中获取所有数组,请使用以下代码行:
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);
}
}
}
}
}
});
输出将是所有文档中的所有参与者。
关于java - Firebase,从所有文档中获取数组列表字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53747054/