简体   繁体   English

检查 Firebase 数据库中是否存在某些键值并执行一些操作 Android

[英]Check if some key value exists in Firebase Database and do some action Android

I want to check if a specific value of a key exists in the Realtime Database or not and perform some action based on it.我想检查实时数据库中是否存在键的特定值,并根据它执行一些操作。

I have the following data:我有以下数据:

"Orders" : {
"03403426747" : {
  "17" : {
    "State" : "(4) Canceled",
    "address" : "yubt",
    "date" : "Feb 28, 2022",
    "discount" : "0",
    "name" : "uk",
    "phone" : "0311111111",
    "time" : "15:33:58 PM",
    "totalAmount" : "3778"
  },
  "18" : {
    "State" : "(1) Approved",
    "address" : "yubt",
    "date" : "Feb 28, 2022",
    "discount" : "120",
    "name" : "uk",
    "phone" : "03111111111",
    "time" : "16:01:58 PM",
    "totalAmount" : "7703"
  }
 }
}

I want to check If any order from these has has "State" value other than "(3) Completed" & "(4) Canceled" .我想检查这些订单中是否有除"(3) Completed""(4) Canceled"以外的"State"值。

if anyone's order has a value other than these, I want to remove that user from the list which contains users with pending orders.如果任何人的订单具有除这些之外的值,我想从包含有挂单的用户的列表中删除该用户。 and if at any time that user has new order or older order State changed I want to again add that user to the list.如果该用户在任何时候有新订单或旧订单 State 发生更改,我想再次将该用户添加到列表中。

I want to check If any order from these has has "State" value other than "(3) Completed" & "(4) Canceled".我想检查这些订单中是否有除“(3) 已完成”和“(4) 已取消”以外的“状态”值。

There is no way you can query the Realtime Database using a negation.您无法使用否定查询实时数据库。 What you can do instead is to create a separate query for each "other" state and join the results on the client.您可以做的是为每个“其他”state 创建一个单独的查询,并在客户端加入结果。

However, if you consider at some point in time to try using Cloud Firestore , then you should consider using not equal (!=) query or if it fits your needs the not-in query.但是,如果您考虑在某个时间点尝试使用Cloud Firestore ,那么您应该考虑使用不等于 (!=) 查询,或者如果它适合您的需要,则使用not-in查询。

After a long time, my brain light burned and I came up with a solution.良久,我脑子里的灯火通明,想出了一个办法。

Create a data class for OrderState为 OrderState 创建数据 class

public class OrderState {
public static int ordersCount = 0;

public static boolean state = false;

public static void update(boolean state){
    if (state){
        ordersCount = ordersCount + 1;
        OrderState.state = true;
    }else
        if (!state && ordersCount > 0){
                ordersCount = ordersCount - 1;
            if (ordersCount < 1) OrderState.state = false;
        }
}

public static void reset(){
    ordersCount = 0;
    state = false;
}
}

On FirebaseRecyclerAdapter -> onBindViewHolder在 FirebaseRecyclerAdapter -> onBindViewHolder

//if order not canceled or completed. it will update OrderState, 
//ordersCount + 1 and state to true

if (adminOrders.getState().equals(Prevalent.orderStateNew) ||
                            adminOrders.getState().equals(Prevalent.orderStateApproved) ||
                            adminOrders.getState().equals(Prevalent.orderStateShipped) ||
                            adminOrders.getState().equals(Prevalent.orderStateApproved)){
                        OrderState.update(true);
                    }

                    changeUserWithOrderState();

On changing state of order by admin关于管理员修改订单state

//if the order is not already cancelled or completed, reduce one order from OrderState as it will be readded automatically upon Recycler refresh.

if (!adminOrders.getState().equals(Prevalent.orderStateCanceled) &&
                                        !adminOrders.getState().equals(Prevalent.orderStateCompleted)) OrderState.update(false);

Al last if the user does not has any order with states New, Approved, and Shipped OrderState.orderCount = 0;最后,如果用户没有任何状态为新、已批准和已发货的订单 OrderState.orderCount = 0; OrderState.state = false; OrderState.state = false; and upon updating the database it will set the state to false.更新数据库后,它将 state 设置为 false。

private void changeUserWithOrderState() {
    DatabaseReference userWithOrder = FirebaseDatabase.getInstance().getReference()
            .child(Prevalent.usersWithOrders)
            .child(userPhoneKey);

   
    HashMap<String, Object> map = new HashMap<>();
    map.put(Prevalent.orderState, String.valueOf(OrderState.state));
    userWithOrder.updateChildren(map).addOnCompleteListener(task -> {
        //Changed state based upon OrderState.state value...
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 检查 FireBase 数据库中是否存在特定值 - Check If a Particular Value Exists in the FireBase Database Using Python 在 React JS 中检查 Firebase 实时数据库中是否存在值 - Check if value exists in Firebase Realtime Database in React JS 如何获取 firebase firestore android 中某些字段的值? - How to get value of some field in firebase firestore android? 如何检查 firebase 数据库中是否存在唯一条目? - How to check if unique entry exists in firebase database or not? 如何将我的数据(在键:值对中)推送到 android 中的 Firebase 实时数据库中? - How do I push my data (in key:value pair) into my Firebase realtime database in android? 如何检查具有某个值的字段的文档是否存在并返回 true/false? - How to check if a document with a field having some value exists and return true/false? 为什么我的 Firebase 数据库的某些属性未定义? - Why are some properties of my Firebase database undefined? 如何检查名称是否已存在于 Firebase 实时数据库(Firebase 和 JS)中 - How to check if name already exists in Firebase Realtime Database (Firebase & JS) Android Firebase,如何检查文档中是否存在子集合 - Android Firebase, How to check if a subcollection exists within a document 如何检查特定用户名是否存在于 firebase 中? (Java 安卓) - How to check if the particular username exists in the firebase? (Java Android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM