[英]Android studio Firebase database queries selected value
I am having trouble trying to query some information in my database, the following picture resumes what information I am trying to fetch, I am trying to get the "productId" 我在尝试查询数据库中的某些信息时遇到麻烦,下图恢复了我要获取的信息,我正在尝试获取“ productId”
This is how my database looks like: 这是我的数据库的样子:
https://i.stack.imgur.com/JhJNs.png https://i.stack.imgur.com/JhJNs.png
In fact, I am trying to get this value in order to put a condition on it. 实际上,我试图获得此值以便为其设置条件。 I am trying to send a notification, ONLY if the productId = 02 is selected, here is my code 我正在尝试发送通知,仅当选择了productId = 02时,这是我的代码
if (request.getFoods().isEmpty()) {
Toast.makeText(Cart.this, "Your cart is empty", Toast.LENGTH_SHORT).show();
} else{
final DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Requests");
final Query data = tokens.orderByChild("productId").equalTo("02"); // get all node with isServerToken
data.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String order_number = String.valueOf(System.currentTimeMillis());
requests.child(order_number).setValue(request);
sendNotificationOrder(order_number);
Toast.makeText(Cart.this, "Notification sent", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thank you for the help! 感谢您的帮助!
Actually your above code would not work with orderByChild()
like that. 实际上,您的上述代码无法与orderByChild()
使用。 If you have multiple items in your foods
node with their parent nodes being 0
, 1
and so on, and you want to check their productId
, then only this can be done. 如果你在你的多个项目foods
节点与他们的父节点是0
, 1
等,并要检查他们productId
,则仅可以做到这一点。
For this, you'd have to make the parent child foods
and call orderByChild()
on it, for productId
. 对于这一点,你就必须让家长儿童foods
和调用orderByChild()
就可以了,为productId
。 The code would look something like this: 代码看起来像这样:
final DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Requests").child(id).child("foods");
tokens.orderByChild("productId").equalTo("02").addSingleValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// do what you want
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Do something for errors, don't ignore
}
However, if you have database structure with different parent nodes and there is one more median node between it and the node you want to retrieve data from, then that won't work with a code like above. 但是,如果您的数据库结构具有不同的父节点,并且在它与您要从中检索数据的节点之间还有一个中间节点,那么上述代码将无法使用。
Learn more about how to make a database structure suitable for orderByChild()
here. 在此处了解有关如何使数据库结构适合orderByChild()
信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.