我的问题是我无法从firebase数据库中删除单个子项,或者没有任何反应或删除整个子项和所有数据。我环顾四周,尝试了多种解决方案,但无论出于什么原因,我都不会工作,而且我也没有想法。
感谢任何帮助。
Java类。
//get data passed from viewpasswords and put them into textviews
name.setText(getIntent().getExtras().getString("data"));
password.setText(getIntent().getExtras().getString("pass"));
//get current user id and reference to database
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
assert user != null;
userID = user.getUid();
dbRef = FirebaseDatabase.getInstance().getReference();
//button onclick stuff
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DeleteData();
}
});
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(DecryptPassword.this, ViewPasswords.class);
startActivity(intent);
}
});
}
private void DeleteData() {
final String passName = name.getText().toString();
Query query = dbRef.child("Passwords").child(userID).orderByChild("PasswordName").equalTo(passName);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getChildrenCount() > 0){
for(DataSnapshot itemSnapshot : dataSnapshot.getChildren()){
dataSnapshot.getRef().removeValue();
Toast.makeText(DecryptPassword.this, "Success", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(DecryptPassword.this, ViewPasswords.class);
startActivity(intent);
}
}else{
Toast.makeText(DecryptPassword.this, "Failed to delete", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(DecryptPassword.this, "Error occurred sorry", Toast.LENGTH_SHORT).show();
}
});
}
Firebase数据库:
感谢。
答案 0 :(得分:3)
dataSnapshot.getRef().removeValue();
将此部分更改为:
itemSnapshot.removeValue();