简体   繁体   中英

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"

This is how my database looks like:

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

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. 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.

For this, you'd have to make the parent child foods and call orderByChild() on it, for 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM