将数据库ref替换为您自己的。但是你如何构建你的评论很糟糕!
private void removeComment() {
try {
DatabaseReference remove = FirebaseDatabase.getInstance().getReference().child
("Comments").child("user_id").child("my_comment_id");
remove.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(this, "Comment deleted", Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
Toast.makeText(this, "Check your connection", Toast.LENGTH_LONG).show();
}
}
尝试使用此代码删除vivek,希望这会有所帮助:
DatabaseReference commentsRef = FirebaseDatabase.getInstance().getReference().child("comments");
commentsRef.child(/* id of the comment */).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap: dataSnapshot.getChildren()) {
String id = snap.getKey();
String username = snap.child("username").getValue(String.class);
if (username.equals("vivek")) {
DatabaseReference removeRef = dataSnapshot.getRef();
removeRef.child(id).removeValue();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});