我现在在从应用程序显示我的参数时遇到问题。 (在此之前我使用随机令牌生成参数。现在我使用新方法(如上图所示)将参数存储在firebase数据库中。 我发现我无法显示参数。我是新来的。谢谢。这是我的代码。
public class RealtimeData extends AppCompatActivity {
private DatabaseReference mDatebase;
private DatabaseReference mDatebase2;
private DatabaseReference mDatebase3;
private DatabaseReference mDatebase4;
private DatabaseReference mDatabase5;
private DatabaseReference mDatabase6;
private DatabaseReference mDatabase7;
private DatabaseReference mDatabase8;
private DatabaseReference mDatabase9;
private TextView mTempView;
private TextView mHumidView;
private TextView mTimeView;
private TextView mphView;
private TextView mwaterView;
private TextView mCO2View;
private TextView mTDSView;
private TextView mDateView;
private TextView mWaterTempView;
@Override
protected void onCreate(Bundle savedInstanceState) {
buildDialog(RealtimeData.this).show();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_realtime_data);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);mDatebase = FirebaseDatabase.getInstance().getReference().child("Region 1").child("Parameter Reading");
mDatebase = FirebaseDatabase.getInstance().getReference().child("Region 1").child("Parameter Reading");
mTempView = (TextView) findViewById(R.id.tempvalue);
mDatebase.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
for (com.google.firebase.database.DataSnapshot datasnap: dataSnapshot.getChildren()){
String temp = datasnap.child("airtemperature").getValue(String.class);
if (Float.valueOf(temp) >= 18 && Float.valueOf(temp) <= 28) {
mTempView.setText("Air Temperature: " + temp + " *C" + " Normal");
}
else if (Float.valueOf(temp) < 18)
{
mTempView.setText("Air Temperature: " + temp + " *C" + " Abnormal Low ");
NotificationCompat.Builder wbuilder = new NotificationCompat.Builder(RealtimeData.this);
wbuilder.setSmallIcon(R.drawable.impressive);
wbuilder.setContentTitle("Notification Alert");
wbuilder.setContentText("Air Temperature lowly abnormal!!");
wbuilder.setWhen(System.currentTimeMillis());
wbuilder.setVibrate(new long[]{1000,1000,1000,1000,1000});
wbuilder.setLights(Color.CYAN,3000,3000);
wbuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
wbuilder.setPriority(NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, wbuilder.build());
}
else if (Float.valueOf(temp) > 28)
{
mTempView.setText("Air Temperature: " + temp + " *C" + " Abnormal High ");
NotificationCompat.Builder wbuilder = new NotificationCompat.Builder(RealtimeData.this);
wbuilder.setSmallIcon(R.drawable.impressive);
wbuilder.setContentTitle("Notification Alert");
wbuilder.setContentText("Air temperature highly abnormal!!");
wbuilder.setWhen(System.currentTimeMillis());
wbuilder.setVibrate(new long[]{1000,1000,1000,1000,1000});
wbuilder.setLights(Color.CYAN,3000,3000);
wbuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
wbuilder.setPriority(NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, wbuilder.build());
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
mTempView.setText("Air Temperature: Error");
}
});
答案 0 :(得分:1)
要解决此问题,请使用以下代码。
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Region 1").child("Parameter Reading");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String airTemperature = dataSnapshot.child("airtemperature").getValue(String.class);
Log.d("TAG", airTemperature);
//Your logic
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);
请记住,没有必要使用getChildren()
方法遍历孩子。您只需要获取引用并获取相应属性的值。