答案 0 :(得分:0)
将数据库引用替换为您自己的。但是,您如何构造评论是不好的!
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();
}
}
答案 1 :(得分:0)
尝试以下代码删除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) {
}
});