簡體   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