Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果没有向下滚动,价格不会增加_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java 如果没有向下滚动,价格不会增加

Java 如果没有向下滚动,价格不会增加,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,购物车列表,我目前确实有一个问题,即当购物车列表中的产品超过4时,总价不包括其余项目的价格,直到我向下滚动,然后总价将添加其余项目的价格,但我希望这是当用户打开它时,它已经显示所有项目的总价,没有滚动。希望有人能帮我找出问题所在,谢谢 以下是购物车活动的代码: public class CartActivity extends AppCompatActivity { private RecyclerView recyclerView; private RecyclerView.LayoutMa

购物车列表,我目前确实有一个问题,即当购物车列表中的产品超过4时,总价不包括其余项目的价格,直到我向下滚动,然后总价将添加其余项目的价格,但我希望这是当用户打开它时,它已经显示所有项目的总价,没有滚动。希望有人能帮我找出问题所在,谢谢

以下是购物车活动的代码:

public class CartActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private TextView txtTotalAmount;
private Button resetbtn;
public CheckBox checkBox;
private static DecimalFormat df = new DecimalFormat("#.00");
private double MemTotalPrice =0;
private double TotalPrice =0;
private boolean count = true;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);


    initWidgets();
    setOnClickListeners();

    recyclerView = findViewById(R.id.cart_list);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    checkBox = findViewById(R.id.checkBox);
    txtTotalAmount = findViewById(R.id.total_price);
}
private void setOnClickListeners(){
    resetbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(CartActivity.this);
            builder.setTitle("Alert");
            builder.setMessage("Are you sure you want to reset?");
            builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DatabaseReference cartListRef =FirebaseDatabase.getInstance().getReference().child("Cart List");
                    cartListRef.removeValue();
                    Intent intent = new Intent(CartActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            });
            android.app.AlertDialog alert2 = builder.create();
            alert2.show();

        }
    });
}
private void initWidgets()
{
    resetbtn = findViewById(R.id.resetbutton);
}

@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));

                   if(oldValue > newValue){
                       TotalPrice = TotalPrice - Double.valueOf(cart.getPrice());
                   }
                   else if(newValue > oldValue){
                       TotalPrice += (Double.valueOf(cart.getPrice()));
                   }
                   count = false;
               }

           });

           if(count){
               TotalPrice += (Double.valueOf(cart.getPrice()) * Double.valueOf(cart.getQuantity()));
           }

           count = true;

            cartViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    CharSequence options [] = new CharSequence[]{

                            "Remove"
                    };
                    AlertDialog.Builder builder = new AlertDialog.Builder(CartActivity.this);
                    builder.setTitle("Cart Options");
                    builder.setItems(options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            if (which == 0)
                            {
                                cartListRef.child("Cart")
                                        .child("Products")
                                        .child(cart.getProductName())
                                        .removeValue()
                                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task)
                                            {
                                                if (task.isSuccessful())
                                                {
                                                    Toast.makeText(CartActivity.this, "Item removed successfully.",Toast.LENGTH_SHORT).show();;

                                                    Intent intent = new Intent(CartActivity.this, MainActivity.class);
                                                    startActivity(intent);
                                                }

                                            }
                                        });
                            }
                        }
                    });
                     builder.show();
                }
            });
           txtTotalAmount.setText("Total Price = RM " + df.format(TotalPrice));
       }

       @NonNull
       @Override
       public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
       {
           View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_items_layout,parent,false);
           CartViewHolder holder = new CartViewHolder(view);
           return holder;
       }
   };
   recyclerView.setAdapter(adapter);
   adapter.startListening();


}
}

以下是截图:

1) 在购物车中,它有5个项目,总价格应为10760,但它显示10750,这是4个项目的总价格,并且没有添加购物车中第5个项目的价格

2) 向下滚动后,添加第5项的价格

请在获取api数据的位置添加活动代码?您必须创建一个函数来计算总价,并将所有产品数据传递给该函数参数,然后运行一个循环来添加每个产品的价格,然后将该总价传递给适配器以将其设置为onview@kam1234这是哪一个api dataFirebase实时数据库发布您的活动代码
{
"Cart List" : {
"Cart" : {
  "Products" : {
    "BenQ" : {
      "MemPrice" : "150",
      "Price" : "300",
      "ProductName" : "BenQ",
      "date" : "19 Jan 2020",
      "quantity" : "1",
      "time" : "23:28:08 p.m."
    },
    "Dell" : {
      "MemPrice" : "5000",
      "Price" : "10000",
      "ProductName" : "Dell",
      "date" : "19 Jan 2020",
      "quantity" : "1",
      "time" : "23:27:13 p.m."
    },
    "IBM" : {
      "MemPrice" : "200",
      "Price" : "400",
      "ProductName" : "IBM",
      "quantity" : "1"
    },
    "KDK" : {
      "MemPrice" : "25",
      "Price" : "50",
      "ProductName" : "KDK",
      "quantity" : "1"
    },
    "Logitech" : {
      "MemPrice" : "5",
      "Price" : "10",
      "ProductName" : "Logitech",
      "date" : "17 Jan 2020",
      "quantity" : "1",
      "time" : "03:07:18 a.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"
  }
 }
}