gpt4 book ai didi

java - 删除 Firebase 数据库 Android 上随机生成的子项

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

remove all random generated child with a button

我想用一个按钮来删除所有生成的 child 。如上图所示,我想使用一个按钮删除所有生成的子项以清理数据库。我之前尝试过几种方法(例如: databasereference.setValue(null) )但显然它无法工作。我是 Android Studio 和 Firebase Databse 的新手。我试过一些例子,但似乎不起作用。我使用了 ID = deletebutton 的按钮这是我的代码。谢谢你的帮助` 公共(public)类 RealtimeData 扩展 AppCompatActivity {

private DatabaseReference mDatebase;
private DatabaseReference mDatebase2;
private DatabaseReference mDatebase3;
private DatabaseReference mDatebase4;
private DatabaseReference mDatabase5;


private TextView mTempView;
private TextView mHumidView;
private TextView mTimeView;
private TextView mphView;


//method that I had tried
private FirebaseAuth.AuthStateListener mAuthListener;
private Button buttonDelete;
private DatabaseReference databasePost;
private FirebaseAuth mAuth;


WifiManager wifiManager;
TextView wifiStatusTextView;


Switch wifiSwitch;
@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);

//declare the deletebutton
mAuth = FirebaseAuth.getInstance();
buttonDelete = (Button) findViewById(R.id.deletebutton);




//method that I tried



mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
if (firebaseAuth.getCurrentUser() != null){
databasePost = FirebaseDatabase.getInstance().getReference().child("Region 1").child("Parameter Reading");
}
}
};

buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mAuth.getCurrentUser() == null || databasePost == null)
return;
databasePost.setValue(null); }
});




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) >= 24 && Float.valueOf(temp) <= 30) {
mTempView.setText("Air Temperature: " + temp + " *C" + " Normal");
}
else if (Float.valueOf(temp) < 24)
{
mTempView.setText("Air Temperature: " + temp + " *C" + " Abnormal Low ");
}

else if (Float.valueOf(temp) > 30)
{
mTempView.setText("Air Temperature: " + temp + " *C" + " Abnormal High ");
}
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
mTempView.setText("Air Temperature: Error");

}
});

mDatebase2 = FirebaseDatabase.getInstance().getReference().child("Region 1").child("Parameter Reading");
mHumidView = (TextView) findViewById(R.id.humidvalue);

mDatebase2.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 humid = datasnap.child("humidity").getValue(String.class);

if (Float.valueOf(humid) >= 70 && Float.valueOf(humid) <= 75) {
mHumidView.setText("Air Humidity: " + humid + " %RH" + " Normal");
}
else if (Float.valueOf(humid) < 70)
{
mHumidView.setText("Air Humidity: " + humid + " %RH" + " Abnormal Low ");
}

else if (Float.valueOf(humid) > 75)
{
mHumidView.setText("Air Humidity: " + humid + " %RH" + " Abnormal High ");
}
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
mHumidView.setText("Air Humidity: Error");
}
});

mDatebase3 = FirebaseDatabase.getInstance().getReference().child("Region 1").child("Parameter Reading");
mTimeView = (TextView) findViewById(R.id.timevalue);

mDatebase3.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 time = datasnap.child("time").getValue(String.class);
//String date = datasnap.child("date").getValue(String.class);
mTimeView.setText("Time: " + time );
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
mTimeView.setText("Time: Error");

}
});




// I removed some of thing which are not important for understanding the problem

最佳答案

删除:

databasePost = FirebaseDatabase.getInstance().getReference().child("Region 1").child("Parameter Reading");

buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
databasePost.removeValue();
}
});

关于java - 删除 Firebase 数据库 Android 上随机生成的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49600982/

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