如何处理我的数据模型类以将Firebase Firestore数据导入recyclerView?

时间:2019-05-26 14:05:20

标签: java firebase android-recyclerview google-cloud-firestore

我有一个像这样的Firestore数据库:

enter image description here

我想访问每个不同的症状数据(即“焦虑数据”)及其由时间戳组成的子项,然后是字典,然后使用RecyclerView {{1}将它们放入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

我不确定如何设置此参数,以便从每个症状数据字段中获取所有Timestamp数组,并将它们全部合并在一个可用于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(); } } 的列表中。 / p>

也许我不能使用FirebaseUI Recycler适配器,或者我需要首先遍历每个不同的症状字段并构建+附加列表?希望我清楚我想做什么。谢谢。

编辑:我已经在iOS上完成了,这是我想要的结果:

enter image description here

编辑:我已经添加了它,获取了每个文档的名称,然后在for循环中添加了该名称之后,我现在尝试获取Array值并将其添加到recyclerView列表中,然后我可以使用自己的适配器:

编辑:这有效,我从每个文档中获取了所需的数据。现在,我只需要能够遍历字段和时间戳,并使用我的模型来创建列表。我怎样才能做到这一点? 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}}

1 个答案:

答案 0 :(得分:0)

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可以将severitysymptomtimestamp属性映射到对象。