Search code examples
androidgoogle-cloud-firestoreandroid-adapterdatasnap

Is it possible to change a DataSnapShot to a Model?


I am using things like this:

int id = getSnapshots().getSnapshot(position).getId().hashCode();  

That works perfectly. No I need to get the model from the DataSnapShot. Is that possible? I tried something like this:

Note note = getSnapshots().getSnapshot(getAdapterPosition())...  

But I don't know how to finish this or even if it' possible. Maybe I need a completely different way?


Solution

  • Assuming that your onBindViewHolder() method has as an argument a ViewHolder object named viewHolder, to solve this, please use the following code:

    DocumentSnapshot snapshot = getSnapshots().getSnapshot(viewHolder.getAdapterPosition());
    Note note = snapshot.toObject(Note.class);
    Log.d("TAG", pm.getNodeId());
    

    Assuming you have in your model class a fied named nodeId and the corresponding getter named getNodeId(), the output in your logcat will be, all the ids of your notes.