How To Represent Nested Data In Firebase Class
Taken from the Firebase example, if I have a Dinosaurs facts data structure like this: { 'lambeosaurus': { 'name': 'lamby', 'work': 'eat', 'dimensions': { 'heig
Solution 1:
The JavaBean class to represent this structure is:
publicstaticclassDinosaurFacts {
String name;
String work;
Dimensions dimensions;
public String getName() {
return name;
}
public String getWork() {
return work;
}
public Dimensions getDimensions() {
return dimensions;
}
publicclassDimensions {
double height;
long weight;
double length;
publicdoublegetHeight() {
return height;
}
publiclonggetWeight() {
return weight;
}
publicdoublegetLength() {
return length;
}
}
}
Then you can read a dino with:
DinosaurFacts dino = dinoSnapshot.getValue(DinosaurFacts.class);
And access the dimensions with for example:
dino.getDimensions().getWeight();
Solution 2:
I followed Frank's answer but I got an error that says:
subclass needs an empty constructor
even though I had one.
Answer :
Create a new (outer) class for it.
Here is my example:
1st Class:
publicclassEvent{
privateString name;
private Place place;
}
Sub Class:
publicclassPlace{
privateString name;
private Location location;
}
Sub of Sub Class:
publicclassLocation{
privateString city;
privateDouble longitude, latitude;
}
Post a Comment for "How To Represent Nested Data In Firebase Class"