我想用一个按钮删除所有生成的子项。 Juz如上图所示,我想用一个按钮删除所有生成的子项,以便清理出数据库。我之前尝试了几种方法(例如:databasereference.setValue(null)),但显然它无法工作。我是Android Studio以及Firebase Databse的新手。我试过一些例子,但似乎没有用。我使用了ID = deletebutton的按钮 这是我的代码。谢谢你的帮助 公共类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
答案 0 :(得分:1)
要删除:
databasePost = FirebaseDatabase.getInstance().getReference().child("Region 1").child("Parameter Reading");
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
databasePost.removeValue();
}
});