我正在使用recyclerView,每行代表一个股票,并且它连接到具有用户身份验证的Firebase帐户,每次创建用户时我都会将他添加到我的Firebase数据库(当他们注册我的Firebase帐户时),每个用户有" user_stocks" (子)字段,我做了一个longClickListener删除我的股票列表中的行,问题是我还需要在用户帐户中删除此股票:
我无法找到从Firebase数据库中删除股票的方法:
public void deleteFromFirebase(String user_id) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("users").child(user_id);
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
//No metter what i did, it did not delete from fb
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}
有人可以指出我正确的方向吗? 谢谢!
答案 0 :(得分:2)
将null放在该子项中作为值
FirebaseDatabase.getInstance().getReference("users").child(user_id).setValue(null);
<强>更新强>
按值删除
public void deleteFromFirebase(String user_id,final String stockName) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("users").child(user_id).child("user_stocks");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot stock : dataSnapshot.getChildren()) {
if(stock.getValue().equals(stockName))
stock.getRef().removeValue();
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}