英文:
Firebase Realtime Database delete item in List
问题
我在实时数据库中以这种格式存储了数据:
如果我想删除列表中的一个条目,名称为7P3MRvkC2FQkwcyC7CYVWZP9Ps72,我会使用这段代码。
此条目下面的id是自动生成的:
FirebaseDatabase.getInstance()
.getReference()
.child("users")
.child(id) -------------------> 唯一id
.orderByChild("orderId")
.equalTo(orderId,"orderId")
.getRef()
.removeValue();
但这会删除完整的用户列表。
英文:
I have the data stored in the Realtime Database in this format:
If I wanted to delete one of the entries in the list named 7P3MRvkC2FQkwcyC7CYVWZP9Ps72, I use this code.
The id under this is generated automatically:
FirebaseDatabase.getInstance()
.getReference()
.child("users")
.child(id) -------------------> unique id
.orderByChild("orderId")
.equalTo(orderId,"orderId")
.getRef()
.removeValue();
But this deletes the complete list of users.
答案1
得分: 1
尝试以下代码:
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference()
.child("users")
.child(id);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// 通知操作成功
}
});
}
});
英文:
Try this code
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference()
.child("users")
.child(id);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// notify
}
});
}
答案2
得分: 0
但是这将删除完整的用户列表。
这是预期的行为。如果您只想删除`orderId`属性保持特定“orderId”值的元素,那么您应该像以下代码中的代码行一样迭代结果:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
DatabaseReference uidRef = usersRef.child(uid);
Query query = uidRef.orderByChild("orderId").equalTo(orderId);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
ds.getRef().removeValue();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //不要忽略潜在错误!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
英文:
> But this deletes the complete list of users.
That's the expected behavior. If you need to remove only the elements where the orderId
property holds the value of a particular "orderId", then you should iterate trough the results like in the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
DatabaseReference uidRef = usersRef.child(uid);
Query query = uidRef.orderByChild("orderId").equalTo(orderId);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
ds.getRef().removeValue();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论