【发布时间】:2019-04-24 15:23:43
【问题描述】:
我是 Google Firebase 的新手,我正在尝试了解它。 我正在做一个 Android 应用程序,您可以在其中创建一组人员并设置组的标题。然后,在“组页面”中,您可以在列表视图中看到您的所有组。 我的 Firestore 数据库的结构是这样的:
用户 --> 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 的文档。
【问题讨论】:
-
请添加您的数据库结构以便更清楚地看到它。
标签: java android firebase google-cloud-firestore