java - 更新产品数量后总价不正确

标签 java android firebase-realtime-database

现在我正在做一个购物车列表,当用户将产品添加到购物车时,我设置数量=1,当用户打开购物车时,它会显示购物车中产品的总价,在这里用户可以增加/减少购物车中的产品数量,总价将一起更新。我现在面临的问题是当我增加或减少产品数量时,显示的总价不正确

以下是 CartActivity 代码:

@Override
public void onStart()
{
    super.onStart();

    final DatabaseReference cartListRef = FirebaseDatabase.getInstance().getReference().child("Cart List");
    FirebaseRecyclerOptions<Cart> options =
            new FirebaseRecyclerOptions.Builder<Cart>()
            .setQuery(cartListRef.child("Cart")
                    .child(("Products")), Cart.class)
                    .build();

   FirebaseRecyclerAdapter<Cart, CartViewHolder> adapter = new FirebaseRecyclerAdapter<Cart, CartViewHolder>(options) {

       @Override
       protected void onBindViewHolder(@NonNull final CartViewHolder cartViewHolder, final int i, @NonNull final Cart cart)
       {
           cartViewHolder.btn_quantity.setNumber(cart.getQuantity());
           cartViewHolder.txtProductPrice.setText("Price = RM"+cart.getPrice());
           cartViewHolder.txtProductName.setText(cart.getProductName());

           cartViewHolder.btn_quantity.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
               @Override
               public void onValueChange(ElegantNumberButton view, int oldValue, int newValue)
               {
                 cart.setQuantity(String.valueOf(newValue));
                 cartListRef.child("Cart").child("Products").child(cart.getProductName()).child("quantity").setValue(String.valueOf(newValue));
                 getTotal();
               }

               private void getTotal()
               {
                   TotalPrice += (Integer.valueOf(cart.getPrice()) * Integer.valueOf(cart.getQuantity()));
               }
           });
           TotalPrice += (Integer.valueOf(cart.getPrice()) * Integer.valueOf(cart.getQuantity()));


}
<小时/>

Firebase 实时数据库:

{
"Cart List" : {
"Cart" : {
  "Products" : {
    "BenQ" : {
      "MemPrice" : "150",
      "Price" : "300",
      "ProductName" : "BenQ",
      "date" : "27 Dec 2019",
      "quantity" : "2",
      "time" : "00:37:56 a.m."
    },
    "Dell" : {
      "MemPrice" : "5000",
      "Price" : "10000",
      "ProductName" : "Dell",
      "date" : "26 Dec 2019",
      "quantity" : "4",
      "time" : "20:52:07 p.m."
    },
    "Logitech" : {
      "MemPrice" : "5",
      "Price" : "10",
      "ProductName" : "Logitech",
      "date" : "26 Dec 2019",
      "quantity" : "1",
      "time" : "20:42:58 p.m."
    }
  }
}
  },
"Product" : {
"Product1" : {
  "Barcode" : "112233",
  "MemPrice" : "5",
  "Price" : "10",
  "ProductName" : "Logitech"
},
"Product2" : {
  "Barcode" : "123456",
  "MemPrice" : "5000",
  "Price" : "10000",
  "ProductName" : "Dell"
},
"Product3" : {
  "Barcode" : "778899",
  "MemPrice" : "150",
  "Price" : "300",
  "ProductName" : "BenQ"
   }
 }
}
<小时/>

我用截图来更清楚地表达我的意思

这是我增加/减少数量之前的图像: Improve this question

这是我已经增加/减少数量的图像: Before increase/decrease

在第一张图片中没有任何问题,但在第二张图片中总价应该是 RM 40610,但它显示 RM 112140

我希望有人可以帮助我找出代码中的问题或教我如何解决此问题。谢谢!

最佳答案

据我所知,导致问题的代码在这里:

cartViewHolder.btn_quantity.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
   @Override
   public void onValueChange(ElegantNumberButton view, int oldValue, int newValue)
   {
     cart.setQuantity(String.valueOf(newValue));
     cartListRef.child("Cart").child("Products").child(cart.getProductName()).child("quantity").setValue(String.valueOf(newValue));
     getTotal();
   }

   private void getTotal()
   {
       TotalPrice += (Integer.valueOf(cart.getPrice()) * Integer.valueOf(cart.getQuantity()));
   }
});
TotalPrice += (Integer.valueOf(cart.getPrice()) * Integer.valueOf(cart.getQuantity()));

最初,您要计算此 View 持有者中商品的价格,并将其添加到总价中:TotalPrice += (Integer.valueOf(cart.getPrice()) * Integer.valueOf(cart.getQuantity( )));

但是每次用户更改数量时,您都会再次将此容器中所有商品的价格添加到总价中。

所以说:

  • 用户一开始购买了 5 件商品,每件售价 20,您将 100 添加到总数中。
  • 然后,用户将同一件商品添加到购物车,最终得到 6 件。您将 6 * 20 = 120 添加到总数中,而您只应为用户添加的一件新商品添加 20 件。<

解决方案是仅添加 oldValuenewValue 项之间的成本差异:

   public void onValueChange(ElegantNumberButton view, int oldValue, int newValue)
   {
     cart.setQuantity(String.valueOf(newValue));
     cartListRef.child("Cart").child("Products").child(cart.getProductName()).child("quantity").setValue(String.valueOf(newValue));

     TotalPrice = TotalPrice + (newValue - oldValue) * cart.getPrice();
   }
<小时/>

一个不相关的建议:您将数值作为字符串存储在数据库中,这会导致代码中进行大量类型转换。如果您将数值存储为实际数字,则可以摆脱所有 Integer.valueOf(...)String.valueOf(...) 调用并使您的代码更短且更易于阅读。

关于java - 更新产品数量后总价不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499011/

相关文章:

android - Android 上的硬件层和硬件加速

java - 如何获取添加到数据库的最后一个节点的值

java - 在 Android Studio Java 中读取数据库标题并显示在 ListView 中

swift - Firebase 将快照值转换为对象

android - firebaseUI 所有共享元素都需要唯一的 transitionNames

java - 从源代码中提取时,我的模块似乎没有在 IntelliJ 中正确设置

java - 无法更改 spring-boot 过滤器映射

java - 在自定义 jtable 中显示结果集

java - 基本tomcat网站: threads or processes?

java - Android - SimpleDateFormat IllegalArgumentException : Unknown pattern character 'u'