简体   繁体   English

Android Studio Java:如果产品已经存在于我的购物车活动中,我该如何更新我的数量?

[英]Android Studio Java: How can I update my quantity if the product already exist's in my Cart Activity?

I am facing an issue which is, I did not know how to update the product quantity where the product already exist in my cart activity.我面临一个问题,即我不知道如何更新产品已存在于我的购物车活动中的产品数量。 I am trying to retrieve my data from firebase, but not able to do so.我正在尝试从 firebase 中检索我的数据,但无法这样做。

Here is my code:这是我的代码:

private void addToCart() {
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User Type").child(firebaseAuth.getUid());
    databaseReference
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                            if (dataSnapshot.child(firebaseAuth.getUid()).child("Cart").child(productID).exists()){
                                //update to firebase
                                String addedQty1 = "" + snapshot.child("addedQuantity").getValue();
                                int getQty = Integer.parseInt(addedQty1);
                                int currentQty = Integer.parseInt(addedQty);

                                int newAddedQty = getQty + currentQty;

                                String addedNewQty = String.valueOf(newAddedQty);

                                HashMap<String, Object> hashMap1 = new HashMap<>();
                                hashMap1.put("addedQuantity", "" + addedNewQty);

                                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User Type");
                                databaseReference.child(firebaseAuth.getUid()).child("Cart").child(productID).updateChildren(hashMap1)
                                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void aVoid) {
                                                //updated
                                                progressDialog.dismiss();
                                                Toast.makeText(ViewProductDetails.this, "Added successfully.", Toast.LENGTH_SHORT).show();
                                            }
                                        })
                                        .addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                //failed to update
                                                progressDialog.dismiss();
                                                Toast.makeText(ViewProductDetails.this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
                                            }
                                        });
                            } else {
                                //Add to firebase
                                final String timestamp = "" + System.currentTimeMillis();

                                HashMap<String, Object> hashMap = new HashMap<>();
                                hashMap.put("cartID", "" + timestamp);
                                hashMap.put("accountID", "" + accountID);
                                hashMap.put("productID", "" + productID);
                                hashMap.put("orderFrom", "" + shopName);
                                hashMap.put("discountNote", "" + discountNote);
                                hashMap.put("originalPrice", "" + oriPrice);
                                hashMap.put("discountPrice", "" + discountedPrice);
                                hashMap.put("price", "" + price);
                                hashMap.put("productTitle", "" + productTitle);
                                hashMap.put("productCategory", "" + category);
                                hashMap.put("productSize", "" + size);
                                hashMap.put("productQuantity", "" + availableQty);
                                hashMap.put("addedQuantity", "" + addedQty);

                                //add to firebase
                                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User Type");
                                databaseReference.child(firebaseAuth.getUid()).child("Cart").child(productID).setValue(hashMap)
                                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void aVoid) {
                                              
                                                progressDialog.dismiss();
                                                Toast.makeText(ViewProductDetails.this, "Added successfully.", Toast.LENGTH_SHORT).show();
                                                
                                            }
                                        })
                                        .addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                //failed to update
                                                progressDialog.dismiss();
                                                Toast.makeText(ViewProductDetails.this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
                                            }
                                        });
                            }
                        }
                    }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });

}

I have shared the code snippet where "else" statement works fine whereas "if" statement is failed to run.我已经分享了代码片段,其中“else”语句工作正常,而“if”语句运行失败。 Hope anyone can help me.希望任何人都可以帮助我。 I really appreciate it!对此,我真的非常感激!

Your basic solution should look like this pseudocode:您的基本解决方案应类似于以下伪代码:

if (product already in cart) {
  get current product count from cart
  increment product count
  store new count back in cart
}

That is one test and three actions.那是一个测试和三个动作。 Check that all four are present and that all four work.检查所有四个都存在并且所有四个都工作。 If you can, it is better to test them separately so you can isolate errors more easily.如果可以,最好单独测试它们,以便更轻松地隔离错误。

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

相关问题 如何在我的活动中获取 viewpager 数据 android java - How can I get viewpager data in my activity android java 如何“更新”我的活动? - How can I “update” my activity? 如何将数据更新为活动? - How can I update my data into my activity? 如何确保我的后台线程绑定到我的 ViewModel 而不是 Zombie Activity? (安卓/Java/MVVM) - How can I ensure my background thread is tied to my ViewModel and not a Zombie Activity? (Android/Java/MVVM) 当我更改活动以继续计算 Android Studio 中的时间时,如何让我的计时器计数? - How can I let my Timer Count when I change the activity to keep counting the time in Android Studio? Android Studio:如何在我的第二个活动中看到intent方法携带的信息? - Android Studio: how can I see the information carried through the intent method on my second activity? Android - 当收到推送通知 FCM 时,如何更新我的活动中的数据或 UI? - Android - How can I update data or UI in my Activity when Push Notification FCM is received? 如何将整数从Java文件发送到XML文件夹? (Android Studio) - How can I send a integer from my Java file to my XML folder? (Android Studio) 如何将我的POS表中的数量减去产品表中的数量 - how to subtract quantity in my POS table to quantity in product table 如何在我的活动中查看寻呼机数据 android java - How can I get view pager data in my activity android java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM