How do I handle my data model class to get Firebase Firestore data into recyclerView?

寵の児 提交于 2021-02-11 14:34:05

问题


I have a Firestore database like this:

I want to access each of the different symptom data's i.e. "Anxiety_data" and its children which consists of timestamps, and then a dictionary, and place them into a RecyclerView using FirebaseUI FirebaseRecyclerViewAdapter

I have this model class:

  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;}

}

Here is my Query:

Query query = db.collection("users").document(user_id).collection("symptom_data");

Here is the 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();

}

}

I am not sure how I should set this up so that I take all the Timestamp arrays from each of the symptom data fields, and have them all together in a list that I can use for the recyclerView.

Maybe I cannot use FirebaseUI Recycler adapter, or I need to iterate through each different symptom field first and build + append a list? Hopefully I am clear in what I would like to do Thank you.

EDIT: I have done it on iOS, This is the result that I want:

EDIT: I have added this, getting the names of each individual document, and then once I have that in a for loop I am now trying to get the Array values and add that to an EntryDataModel list, then I can use my own adapter:

EDIT: This works, I get the data I need, from each Document. Now I just need to be able to Iterate over the fields and timestamps, and use my model to create a list. How can I do that? Log.d("example", "DocumentSnapshot data: " + document.getData()); this prints: 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}} Now I just need to get each timestamp array and add them to a seperate list, and i can then do this for each document and have a full list of all the timestamp arrays.

  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());
             }
         }
     });

}

回答1:


The automatic mapping that the Firebase SDK can do from a Firestore document to a Java object, requires that each field name from the document matches a property name in the Java class. It has no mechanism to deal with dynamic fields, such as the timestamps in your example.

So you will have to do your own conversion. For that you can use the public T get (FieldPath fieldPath, Class<T> valueType) or public T get (String field, Class<T> valueType) method, which allow you to get an object from a specific field that you specify. So you will have to loop over the timestamp fields, but after that the Firebase SDK can map the severity, symptom, and timestamp properties to the object.



来源:https://stackoverflow.com/questions/56314221/how-do-i-handle-my-data-model-class-to-get-firebase-firestore-data-into-recycler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!