繁体   English   中英

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

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

我面临一个问题,即我不知道如何更新产品已存在于我的购物车活动中的产品数量。 我正在尝试从 firebase 中检索我的数据,但无法这样做。

这是我的代码:

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) {

                }
            });

}

我已经分享了代码片段,其中“else”语句工作正常,而“if”语句运行失败。 希望任何人都可以帮助我。 对此,我真的非常感激!

您的基本解决方案应类似于以下伪代码:

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

那是一个测试和三个动作。 检查所有四个都存在并且所有四个都工作。 如果可以,最好单独测试它们,以便更轻松地隔离错误。

暂无
暂无

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

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