This is my database :
[x] database
-- > [+] students
-----> [+] -KuGacomJymBwkcc7zlU (pushKey)
-----------> Name - Jon
-----------> Age - 21
I have Student.class :
String name;
String age;
public Student(String Name, String Age) {
this.name=Name;
this.age=Age;
}
I read the information from firebase datasnapshot like this:
Student newStudent = DataSnapshot.getValue(Student.class);
When I do this I get the name and the age, but my question if there is a way to store the ID (pushKey) on the Student class without adding a String that will hold it and take another field on the firebase database.
Thank you all.
I prefer keeping the keys and the values separate, or otherwise passing the DataSnapshot
s around. But if you want, you can also extract the key (DataSnapshot.getKey()
) and set it on a property in your Java class that you @Exclude
:
public class Student {
@Exclude
public String key;
public String name;
public String age;
}
And then:
Student newStudent = snapshot.getValue(Student.class);
newStudent.key = snapshot.getKey();
Related:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With