我在Java中有以下代码,可从Firebase中提取数据:
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
FirebaseData ShiftMap = child.getValue(FirebaseData.class);
}
}
模型类Firebase数据为:
@IgnoreExtraProperties
public class FirebaseData {
public String Operator;
public double Ora00;
public double Ora01;
public double Ora02;
public double Ora03;
public FirebaseData() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public FirebaseData(String operator, double ora00, double ora01, double ora02, double ora03)
{
this.Operator = operator;
this.Ora00 = ora00;
this.Ora01 = ora01;
this.Ora02 = ora02;
this.Ora03 = ora03;
}
}
我如何遍历每个项目以检查其价值。
现在我正以这种方式这样做,但是我认为这不是正确的方式:
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
FirebaseData ShiftMap = child.getValue(FirebaseData.class);
if (!Map.Operator.isEmpty())
{
if (ShiftMap .Ora00 == 0)
{
//Do Something
}
else if (ShiftMap .Ora00 == 0.5)
{
//Do Something
}
else if (ShiftMap .Ora00 == 1)
{
//Do Something
}
}
编辑
@AlexMamo要求的是数据库结构:
[
{
"Operator": "someone",
"Ora00": 0,
"Ora01": 0,
"Ora02": 0,
"Ora03": 0,
},
{
"Operator": "someone",
"Ora00": 0,
"Ora01": 0,
"Ora02": 0,
"Ora03": 0,
}
]