[英]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.