我尝试使用removeValue()从我的FirebaseDatabase中删除一个节点,但它不起作用。奇怪的是,当我在Recyclerview中删除它时节点已成功删除。
我使用下面的代码在一个新的活动中通过onclick传递来自Recyclerview的引用。
final DatabaseReference itemRef = getRef(position);
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// itemRef.removeValue();
Intent intentshowall2one= new Intent(Showcase.this, ShowProduct.class);
intentshowall2one.putExtra("S2Skey", itemRef.toString());
Showcase.this.startActivity(intentshowall2one);
以及其他活动
key = myIntent.getStringExtra("S2Skey");
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl(key);
mDatabase.removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT)
.show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT)
.show();
}
});
答案 0 :(得分:0)
在你的按钮或任何听众中试试这个:
mDatabase.child(key).removeValue();
Toast.makeText(getApplicationContext(),"The Post has been removed Successfully ",Toast.LENGTH_LONG).show();
或使用此
mDatabase.chlid(key).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT)
.show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT)
.show();
}
});
代替:
mDatabase.removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT)
.show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT)
.show();
}
});
希望它对您有用
答案 1 :(得分:0)
在删除值之前,请确保已给出正确的引用。然后,
您可以使用Completion侦听器而不是OnSuccessListener,完成侦听器可以提供准确的结果。
答案 2 :(得分:0)
感谢您的回答Lutaaya Huzaifah Idris和Vignesh Kumar。我发现错误是因为我在我的路径中使用希腊语单词。一切正常上传下载并用希腊语单词查询ref但是当我将ref传递给裁判改变的另一项活动。这也很奇怪。