我想从Firebase数据库中删除“全部”节点。
这是我的数据库结构:
这是默认生成的节点。我没有在代码中的任何地方添加它。下面是我将数据插入Firebase数据库中的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_details);
etemail_id = (EditText) findViewById(R.id.etEmailid);
etpassword = (EditText) findViewById(R.id.etPassword);
tvlogin_details = (TextView) findViewById(R.id.tvLoginDetails);
btback = (Button) findViewById(R.id.btBack);
mAuth = FirebaseAuth.getInstance();
btregister = (Button) findViewById(R.id.btRegister);
database=FirebaseDatabase.getInstance();
reference=database.getReference();
users=new Users();
btregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String email = etemail_id.getText().toString().trim();
final String password = etpassword.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
etemail_id.setError("Provide your Email first!");
etemail_id.requestFocus();
}
else if (TextUtils.isEmpty(password)) {
etpassword.setError("Enter Password!");
etpassword.requestFocus();
}
else if (!(email.isEmpty() && password.isEmpty())) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(LoginDetails.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
SharedPreferences sp = getSharedPreferences("Mypref", Context.MODE_PRIVATE);
reference.child("User").child(mAuth.getUid()).setValue(sp);
Toast.makeText(getApplicationContext(), "data entered", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "database error", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(LoginDetails.this, "Successfully registered", LENGTH_LONG).show();
Intent it = new Intent(getApplicationContext(), Login.class);
startActivity(it);
}
else {
Toast.makeText(LoginDetails.this, "Registration Error", LENGTH_LONG).show();
}
}
});
}
else {
Toast.makeText(LoginDetails.this, "Error", Toast.LENGTH_SHORT).show();
}
}
});
}
请检查出现了什么问题。
答案 0 :(得分:2)
根据您的评论,要删除all
节点,请使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference allRef = rootRef.child("User").child(uid).child("all");
allRef.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "all node deleted");
}
}
});;