简体   繁体   English

Android Studio Firebase数据库查询所选值

[英]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节点与他们的父节点是01等,并要检查他们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.

相关问题 android studio 只检查和检索 firebase 数据库中的最后一个添加值 - android studio only checks and retrieve the last added value in firebase database Android Studio Firebase数据库检索多个子项的子项值 - Android studio firebase database retrieve child value for multiple childs 使用Android Studio和Firebase在Spinner中获取其选定的子值的键 - Getting the key of its selected child value in a Spinner using Android Studio and Firebase 如何在Firebase数据库Android上删除选定的孩子 - How to delete selected child on Firebase Database Android 如何在Android的Firebase数据库中获取具有选定值的节点的密钥UID? - How do i get the key UID of a node with a selected value in firebase database in android? Android Firebase 数据库获取值 - Android Firebase Database get value 如何使用Android Studio在模型中创建一个增量数字字符串值并在Firebase数据库中读取该值? - How to create an Increment Numeric String Value inside model and read the value inside Firebase Database using Android Studio? 如何在Android Studio中的同一ArrayAdaptor中查看2个Firebase查询的结果 - How to view results of 2 Firebase queries in the same ArrayAdaptor in Android Studio Firebase 通过多个查询搜索并添加到 RecyclerView Android Studio - Firebase search by multiple queries and add to RecyclerView Android Studio Android Studio Firebase 数据库数据快照返回 null - Android Studio Firebase Database DataSnapshot returning null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM