【发布时间】:2019-10-12 07:55:35
【问题描述】:
我有一个这样的 Firestore 数据库:
我想访问每个不同的症状数据,即“Anxiety_data”及其由时间戳和字典组成的子项,然后使用FirebaseUIFirebaseRecyclerViewAdapter将它们放入RecyclerView中
我有这个模型类:
public class EntryDataModel {
private String timestamp, symptom, severity, comment;
public EntryDataModel() {}
public EntryDataModel(String timestamp, String symptom, String severity, String comment) {
this.timestamp = timestamp;
this.symptom = symptom;
this.severity = severity;
this.comment = comment;
}
public String getTimestamp() {return timestamp;}
public String getSymptom() {return symptom;}
public String getSeverity() {return severity;}
public String getComment() {return comment;}
}
这是我的Query:
Query query = db.collection("users").document(user_id).collection("symptom_data");
这里是Firebase RecyclerView Adapter:
void fireStoreRecyclerAdapterSetup() {
FirestoreRecyclerOptions<EntryDataModel> options = new FirestoreRecyclerOptions.Builder<EntryDataModel>()
.setQuery(query, EntryDataModel.class)
.build();
FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<EntryDataModel, EntryDataHolder>(options) {
@Override
public void onBindViewHolder(EntryDataHolder holder, int position, EntryDataModel model) {
// Bind the Chat object to the ChatHolder
// ...
System.out.println("Query: " + query.toString());
}
@Override
public EntryDataHolder onCreateViewHolder(ViewGroup group, int i) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.entry_data_recyclerview_item, group, false);
return new EntryDataHolder(view);
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
}
我不知道应该如何设置,以便从每个症状数据字段中获取所有时间戳数组,并将它们全部放在一个列表中,我可以将它们用于recyclerView。
也许我不能使用 FirebaseUI Recycler 适配器,或者我需要先遍历每个不同的症状字段并构建 + 附加列表?希望我清楚我想做什么谢谢。
编辑:我已经在 iOS 上完成了,这是我想要的结果:
编辑:我已经添加了这个,获取每个文档的名称,然后一旦我在 for 循环中得到它,我现在尝试获取 Array 值并将其添加到 EntryDataModel 列表中,然后我可以使用我自己的适配器:
编辑:这行得通,我从每个文档中获取我需要的数据。现在我只需要能够迭代字段和时间戳,并使用我的模型创建一个列表。我怎样才能做到这一点? Log.d("example", "DocumentSnapshot data: " + document.getData()); 打印:D/example: DocumentSnapshot data: {1558879769={severity=Mild, symptom=Anxiety, comment=Mild anxiety at work., timestamp=1558879769}, 1558879745={severity=Mild, symptom=Anxiety, comment=Feeling relaxed watching TV., timestamp=1558879745}, 1558879710={severity=Moderate, symptom=Anxiety, comment=Walking the cat., timestamp=1558879710}, 1558879827={severity=Moderate, symptom=Anxiety, comment=Taking the cat for a walk., timestamp=1558879827}, 1558888729={severity=Mild, symptom=Anxiety, comment=The cat is fed up with walking., timestamp=1558888729}} 现在我只需要获取每个时间戳数组并将它们添加到单独的列表中,然后我可以为每个文档执行此操作并获得所有时间戳数组的完整列表。
void getSymptomData() {
final CollectionReference colRef = db.collection("users").document(user_id).collection("symptom_data");
colRef.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());
DocumentReference docRef = colRef.document(document.getId());
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d("Iteration", "DocumentSnapshot data: " + document.getData());
} else {
Log.d("NoDoc", "No such document");
}
} else {
Log.d("Failed", "get failed with ", task.getException());
}
}
});
}
Log.d("listylist", list.toString());
} else {
Log.d("tag", "Error getting documents: ", task.getException());
}
}
});
}
【问题讨论】:
标签: java firebase android-recyclerview google-cloud-firestore