3 回答
TA贡献1820条经验 获得超10个赞
试试这个代码:
private void deleteTask (final String userName){
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("All_USER");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds :dataSnapshot.getChildren()){
ds.getRef().orderByChild("userName").equalTo(userName).removeValue();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
TA贡献1876条经验 获得超7个赞
这段代码对我有用
private void deleteTask (final String userName){
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("All_USER");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds :dataSnapshot.getChildren()){
if(ds.child("NewUser").child("userName").getValue(String.class).equals(userName)){
ds.child("NewUser").child("userName").getRef().removeValue();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
TA贡献1906条经验 获得超10个赞
您可以像这样更改代码:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (snapshot in dataSnapshot.children) {
snapshot.reference.removeValue()
}
}
}
添加回答
举报