android 购物车实现,Android Studio实现简单购物车功能 显示代码
时间: 2024-02-23 07:57:46 浏览: 109
Android Studio实现简单购物车功能
好的,以下是一个简单的购物车功能的示例代码,包括商品列表、购物车列表和计算总价的代码。
1. 商品列表代码
```java
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private List<Product> productList;
public ProductAdapter(List<Product> productList) {
this.productList = productList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Product product = productList.get(position);
holder.tvName.setText(product.getName());
holder.tvPrice.setText(String.valueOf(product.getPrice()));
holder.btnAddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cart.getInstance().addProduct(product);
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
TextView tvPrice;
Button btnAddToCart;
ViewHolder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
tvPrice = itemView.findViewById(R.id.tv_price);
btnAddToCart = itemView.findViewById(R.id.btn_add_to_cart);
}
}
}
```
2. 购物车列表代码
```java
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {
private List<Product> productList;
public CartAdapter(List<Product> productList) {
this.productList = productList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Product product = productList.get(position);
holder.tvName.setText(product.getName());
holder.tvPrice.setText(String.valueOf(product.getPrice()));
holder.tvQuantity.setText(String.valueOf(product.getQuantity()));
holder.btnIncrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cart.getInstance().addProduct(product);
notifyDataSetChanged();
}
});
holder.btnDecrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cart.getInstance().removeProduct(product);
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
TextView tvPrice;
TextView tvQuantity;
Button btnIncrease;
Button btnDecrease;
ViewHolder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
tvPrice = itemView.findViewById(R.id.tv_price);
tvQuantity = itemView.findViewById(R.id.tv_quantity);
btnIncrease = itemView.findViewById(R.id.btn_increase);
btnDecrease = itemView.findViewById(R.id.btn_decrease);
}
}
}
```
3. 计算总价代码
```java
public class Cart {
private List<Product> productList;
private static Cart instance;
private Cart() {
productList = new ArrayList<>();
}
public static synchronized Cart getInstance() {
if (instance == null) {
instance = new Cart();
}
return instance;
}
public void addProduct(Product product) {
if (productList.contains(product)) {
int index = productList.indexOf(product);
Product p = productList.get(index);
p.setQuantity(p.getQuantity() + 1);
} else {
product.setQuantity(1);
productList.add(product);
}
}
public void removeProduct(Product product) {
if (productList.contains(product)) {
int index = productList.indexOf(product);
Product p = productList.get(index);
if (p.getQuantity() == 1) {
productList.remove(index);
} else {
p.setQuantity(p.getQuantity() - 1);
}
}
}
public double getTotalPrice() {
double totalPrice = 0.0;
for (Product product : productList) {
totalPrice += product.getPrice() * product.getQuantity();
}
return totalPrice;
}
public List<Product> getProductList() {
return productList;
}
public void clear() {
productList.clear();
}
}
```
以上是一个简单的购物车功能的示例代码,仅供参考。具体实现过程可以根据个人需求进行调整和改进。
阅读全文