用Android实现一个购物车,以列表的形式显示商品信息,,并可以对商品进行加入购物车、删除、修改数量和查询
时间: 2024-06-12 11:08:09 浏览: 16
商品价格等操作。
首先,我们需要创建一个商品类,包含商品名称、价格和数量等属性:
```
public class Product {
private String name;
private double price;
private int quantity;
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return name + " - " + price + " - " + quantity;
}
}
```
接下来,我们创建一个购物车类,用于管理商品列表:
```
public class ShoppingCart {
private List<Product> productList;
public ShoppingCart() {
productList = new ArrayList<>();
}
public void addProduct(Product product) {
productList.add(product);
}
public void removeProduct(Product product) {
productList.remove(product);
}
public void updateProductQuantity(Product product, int quantity) {
product.setQuantity(quantity);
}
public List<Product> getProductList() {
return productList;
}
}
```
接下来,我们需要创建一个布局文件,用于显示商品列表。在布局文件中,我们可以使用RecyclerView来显示商品列表,并为每个商品添加一个加入购物车按钮:
```
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/product_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/add_to_cart_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to Cart"/>
```
接下来,我们需要创建一个适配器类,用于将商品列表显示在RecyclerView中:
```
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private List<Product> productList;
private OnAddToCartClickListener listener;
public ProductAdapter(List<Product> productList, OnAddToCartClickListener listener) {
this.productList = productList;
this.listener = listener;
}
@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.nameTextView.setText(product.getName());
holder.priceTextView.setText(String.valueOf(product.getPrice()));
holder.quantityTextView.setText(String.valueOf(product.getQuantity()));
holder.addToCartButton.setOnClickListener(view -> listener.onAddToCartClick(product));
}
@Override
public int getItemCount() {
return productList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView nameTextView;
private TextView priceTextView;
private TextView quantityTextView;
private Button addToCartButton;
public ViewHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.product_name);
priceTextView = itemView.findViewById(R.id.product_price);
quantityTextView = itemView.findViewById(R.id.product_quantity);
addToCartButton = itemView.findViewById(R.id.add_to_cart_button);
}
}
public interface OnAddToCartClickListener {
void onAddToCartClick(Product product);
}
}
```
最后,我们需要在MainActivity中使用上述类来实现购物车的基本功能:
```
public class MainActivity extends AppCompatActivity implements ProductAdapter.OnAddToCartClickListener {
private ShoppingCart shoppingCart;
private ProductAdapter productAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shoppingCart = new ShoppingCart();
RecyclerView productList = findViewById(R.id.product_list);
productAdapter = new ProductAdapter(getProductList(), this);
productList.setAdapter(productAdapter);
productList.setLayoutManager(new LinearLayoutManager(this));
}
private List<Product> getProductList() {
List<Product> productList = new ArrayList<>();
productList.add(new Product("Product 1", 10.0, 5));
productList.add(new Product("Product 2", 20.0, 3));
productList.add(new Product("Product 3", 30.0, 2));
return productList;
}
@Override
public void onAddToCartClick(Product product) {
shoppingCart.addProduct(product);
productAdapter.notifyDataSetChanged();
}
}
```
在MainActivity中,我们创建了一个ShoppingCart对象,并将商品列表显示在RecyclerView中。当用户点击加入购物车按钮时,我们将商品添加到购物车中,并更新RecyclerView中的商品列表。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)