我如何将具有相同名称的所有项目计为一个项目。如果使用添加按钮向购物车中添加商品的时间更多,则我在购物车中具有相同名称的商品获得的时间更多。如果我再次添加相同的商品,我该如何编码数量会增加?
这是Cart.java
private void loadListFood() {
cart = new Database(this).getCarts();
adapter = new CartAdapter(cart, this);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
//Calculate total
double total = 0;
for (Order order:cart)
total+=(Double.parseDouble(order.getPrice()))*(Integer.parseInt(order.getQuantity()));
Locale locale = new Locale("de", "DE");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
txtTotalPrice.setText(fmt.format(total));
}
这是Food.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
//Firebase
database = FirebaseDatabase.getInstance();
food = database.getReference("Food");
//init view
numberButton = (ElegantNumberButton)findViewById(R.id.number_button);
btnCart =(FloatingActionButton) findViewById(R.id.btnCart);
btnCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Database(getBaseContext()).addToCart(new Order(
foodId,
currentFood.getName(),
numberButton.getNumber(),
currentFood.getPrice(),
currentFood.getDiscount()
));
Toast.makeText(FoodDetail.this, "Hinzugefügt!", Toast.LENGTH_SHORT).show();
}
});
food_description = (TextView)findViewById(R.id.food_description);
food_name = (TextView)findViewById(R.id.food_name);
food_price = (TextView)findViewById(R.id.food_price);
food_image = (ImageView)findViewById(R.id.img_food);
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.CollapseddAppbar);
//Get Food Id from Intent
if (getIntent() != null)
foodId = getIntent().getStringExtra("foodId");
if(!foodId.isEmpty())
{
if (Common.isConnectedToInternet(getBaseContext()))
getDetailFood(foodId);
else{
Toast.makeText(FoodDetail.this, "Stellen Sie bitte sicher, das eine aktive Internetverbindung vorhanden ist, und versuchen Sie es erneut!", Toast.LENGTH_SHORT).show();
return;
}
});
}