Firestore不会删除文档-Android

时间:2019-06-27 16:04:51

标签: android google-cloud-firestore

我正在开发商店应用。有一个RecyclerView,显示添加到购物车中的物品。每个项目都有一个复选框。选中复选框后,该项目应添加到Firestore。到现在为止,这有效。但是不起作用的是,如果未选中该复选框,则用户不想使用该项目下订单,则应从Firestore中删除该文档,而不是这种情况。

这是代码:

holder.cartCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {

            int adapterPosition = holder.getAdapterPosition();

            if (firebaseUser != null) {

                userID = firebaseUser.getUid();

                char[] chars1 = "0123456789".toCharArray();

                StringBuilder stringBuilder = new StringBuilder();
                Random random = new Random();
                for (int i = 0; i < 16; i++) {
                    char c1 = chars1[random.nextInt(chars1.length)];
                    stringBuilder.append(c1);
                }
                final String order_id = stringBuilder.toString();

                final DocumentReference priceDocument = firebaseFirestore.collection("Shopping Cart").document(userID).collection("Total Amount").document("price");

                if (!itemStateArray.get(adapterPosition, false)) {

                    holder.cartCheckBox.setChecked(true);
                    itemStateArray.put(adapterPosition, true);
                    // Toast.makeText(context, "You selected: " + cartItemID + " " + "(" + name + ")", Toast.LENGTH_LONG).show();

                    priceDocument.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {

                            if (task.isSuccessful()) {

                                DocumentSnapshot documentSnapshot = task.getResult();

                                if (documentSnapshot.exists()) {

                                    String oldPrice = task.getResult().getString("price");

                                    int priceInteger = Integer.parseInt(price) + Integer.parseInt(oldPrice);

                                    String finalPrice = Integer.toString(priceInteger);

                                    myCallback.priceListenerMethod(finalPrice);

                                    Map<String, Object> priceMap = new HashMap<>();
                                    priceMap.put("price", finalPrice);

                                    priceDocument.set(priceMap);

                                    /*
                                    orderCallBack.orderListenerMethod("image_url: " + image_url);
                                    orderCallBack.orderListenerMethod("name: " + name);
                                    orderCallBack.orderListenerMethod("size: " + size);
                                    orderCallBack.orderListenerMethod("price_checked: " + price);
                                    orderCallBack.orderListenerMethod("final_price_checked: " + finalPrice);
                                    */

                                    Map<String, Object> orderMap = new HashMap<>();
                                    orderMap.put("image", image_url);
                                    orderMap.put("name", name);
                                    orderMap.put("size", size);
                                    orderMap.put("color", color);
                                    orderMap.put("price", price);
                                    orderMap.put("total", finalPrice);

                                    firebaseFirestore.collection("Orders").document(userID).collection("Order ID").document(order_id).set(orderMap, SetOptions.merge());

                                } else {

                                    Map<String, Object> priceMap = new HashMap<>();
                                    priceMap.put("price", price);

                                    myCallback.priceListenerMethod(price);

                                    priceDocument.set(priceMap);

                                    Map<String, Object> orderMap = new HashMap<>();
                                    orderMap.put("image", image_url);
                                    orderMap.put("name", name);
                                    orderMap.put("size", size);
                                    orderMap.put("color", color);
                                    orderMap.put("price", price);

                                    firebaseFirestore.collection("Orders").document(userID).collection("Order ID").document(order_id).set(orderMap);

                                }

                            }

                        }
                    });

                } else {

                    holder.cartCheckBox.setChecked(false);
                    itemStateArray.put(adapterPosition, false);

                    priceDocument.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {

                            if (task.isSuccessful()) {

                                DocumentSnapshot documentSnapshot = task.getResult();

                                if (documentSnapshot.exists()) {

                                    String oldPrice = task.getResult().getString("price");

                                    int priceInteger = Integer.parseInt(oldPrice) - Integer.parseInt(price);

                                    String finalPrice = Integer.toString(priceInteger);

                                    myCallback.priceListenerMethod(finalPrice);

                                    Map<String, Object> priceMap = new HashMap<>();
                                    priceMap.put("price", finalPrice);

                                    priceDocument.set(priceMap);

                                    /*
                                    Map<String, Object> orderMap = new HashMap<>();
                                    orderMap.put("image", image_url);
                                    orderMap.put("name", name);
                                    orderMap.put("size", size);
                                    orderMap.put("color", color);
                                    orderMap.put("price", price);
                                    orderMap.put("total", finalPrice);
                                    */

                                    // firebaseFirestore.collection("Orders").document(userID).collection("Order ID").document(order_id).delete();

                                    /*
                                    orderCallBack.orderListenerMethod("image_url: " + image_url);
                                    orderCallBack.orderListenerMethod("name: " + name);
                                    orderCallBack.orderListenerMethod("size: " + size);
                                    orderCallBack.orderListenerMethod("color: " + color);
                                    orderCallBack.orderListenerMethod("price_UNchecked: " + price);
                                    orderCallBack.orderListenerMethod("final_price_UNchecked: " + finalPrice);
                                    */

                                } else {

                                    firebaseFirestore.collection("Orders").document(userID).collection("Order ID").document(order_id).delete();
                                    Toast.makeText(context, order_id + " removed", Toast.LENGTH_SHORT).show();

                                }

                            }

                        }
                    });

                }

            }

        }

    });

您会注意到我正在使用回调。我尝试使用它来将项目数据发送到该适配器的活动。但是我不知道如何。

请帮忙吗?

0 个答案:

没有答案