gpt4 book ai didi

java - 更新参数子项后更改值(不生成随机 token )Firebase Android Studio

转载 作者:行者123 更新时间:2023-11-30 00:02:22 26 4
gpt4 key购买 nike

The parameter child is updated without random token generated

我现在无法从应用程序显示我的参数。 (在此之前我使用随机 token 生成参数。现在我使用新方法(如上传的图片所示)将参数存储在 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");
}
});

最佳答案

要解决此问题,请使用以下代码。

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() 方法遍历子对象。您只需要获取引用并获取相应属性的值即可。

关于java - 更新参数子项后更改值(不生成随机 token )Firebase Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com