问题描述
从Firebase的例子来看,如果我有这样一个恐龙事实数据结构:$ p $
{
lambeosaurus:{
name:lamby,
work:eat,
dimensions:{
height:2.1,
长度:12.5,
重量:5000
}
},
剑龙:{
name:stego,
work:play,
dimensions:{
height:4,
length:9,
weight:2500
$用于Firebase的Android类从 DataSnapshot.getValue(DinosaurFacts.class)投射
?
名称和工作作为字符串,但如何表示类中的维度集合?
另外我怎样才能从 DataSnapshot
关于身高&重量?
编辑
我可以通过快照获取单个元素循环,但是试图找到如何表示数据(DataSnapshot child:dataSnapshot.getChildren()){
Log();
.d(hz-key:,child.getKey()。toString());
Log.d(hz-val:,child.getValue()。toString());
$ / code $ / pre
解决方案结构是:
public static class DinosaurFacts {
String name;
字符串工作;
尺寸尺寸;
public String getName(){
return name;
}
public String getWork(){
return work;
}
公共维度getDimensions(){
return dimensions;
}
public class Dimensions {
double height;
长的重量;
双倍长度;
public double getHeight(){
return height;
}
public long getWeight(){
return weight;
}
public double getLength(){
return length;
然后你可以读一个恐龙与:
DinosaurFacts dino = dinoSnapshot.getValue(DinosaurFacts.class);
访问维度,例如:
dino.getDimensions()getWeight();
Taken from the Firebase example, if I have a Dinosaurs facts data structure like this:
{
"lambeosaurus": {
"name": "lamby",
"work": "eat",
"dimensions": {
"height" : 2.1,
"length" : 12.5,
"weight": 5000
}
},
"stegosaurus": {
"name": "stego",
"work": "play",
"dimensions": {
"height" : 4,
"length" : 9,
"weight" : 2500
}
}
}
How can I represent this structure in a Android Class for Firebase to cast from DataSnapshot.getValue(DinosaurFacts.class)
?
name, and work are represented as Strings, but how to represent "dimensions" collection in the class?
Also how can I access data values from DataSnapshot
about height & weight?
EDITI can get the individual elements looping through the snapshot, but am trying to find how to represent the data in the class structure.
for(DataSnapshot child : dataSnapshot.getChildren()) {
Log.d("hz-key:", child.getKey().toString());
Log.d("hz-val:", child.getValue().toString());
}
解决方案 The JavaBean class to represent this structure is:
public static class DinosaurFacts {
String name;
String work;
Dimensions dimensions;
public String getName() {
return name;
}
public String getWork() {
return work;
}
public Dimensions getDimensions() {
return dimensions;
}
public class Dimensions {
double height;
long weight;
double length;
public double getHeight() {
return height;
}
public long getWeight() {
return weight;
}
public double getLength() {
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();
这篇关于如何在Firebase类中表示嵌套数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!