我有一个像这样的 Firestore 数据库:
我想访问每个不同的症状数据,即“Anxiety_data”及其子数据,其中包含时间戳,然后是字典,并将它们放入 RecyclerView
中。使用FirebaseUI
FirebaseRecyclerViewAdapter
我有这个模型类:
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 循环中,我现在尝试获取数组值并将其添加到 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());
}
}
});
}
最佳答案
Firebase SDK 可以从 Firestore 文档到 Java 对象进行自动映射,要求文档中的每个字段名称与 Java 类中的属性名称相匹配。它没有处理动态字段的机制,例如示例中的时间戳。
所以你必须自己进行转换。为此,您可以使用 public T get (FieldPath fieldPath, Class<T> valueType)
or public T get (String field, Class<T> valueType)
method ,它允许您从指定的特定字段获取对象。因此,您必须循环遍历时间戳字段,但之后 Firebase SDK 可以映射 severity
, symptom
,和timestamp
对象的属性。
关于java - 如何处理我的数据模型类以将 Firebase Firestore 数据导入 recyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56314221/