android 购物车实现,Android Studio实现简单购物车功能
时间: 2023-08-05 17:27:04 浏览: 89
Android Studio实现简单购物车功能
5星 · 资源好评率100%
好的,我可以给你一些实现购物车功能的思路和步骤:
1. 创建一个商品类,包含商品的名称、价格、数量等属性;
2. 创建一个购物车类,包含商品列表、添加商品、删除商品、计算总价等方法;
3. 在界面上显示商品列表,并提供添加到购物车的按钮;
4. 当添加到购物车时,将商品对象添加到购物车中,并更新购物车的显示;
5. 在购物车界面上显示购物车中的商品列表、数量、价格等信息;
6. 可以提供删除商品、修改数量、清空购物车等操作;
7. 计算购物车中的总价,并显示在界面上。
这些步骤可以通过以下代码实现:
1. 商品类
```
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 double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
```
2. 购物车类
```
public class ShoppingCart {
private List<Product> productList = new ArrayList<>();
public void addProduct(Product product) {
productList.add(product);
}
public void removeProduct(Product product) {
productList.remove(product);
}
public double getTotalPrice() {
double totalPrice = 0;
for (Product product : productList) {
totalPrice += product.getPrice() * product.getQuantity();
}
return totalPrice;
}
public List<Product> getProductList() {
return productList;
}
}
```
3. 商品列表界面
```
public class ProductListActivity extends AppCompatActivity {
private List<Product> productList = new ArrayList<>();
private RecyclerView recyclerView;
private ProductListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
// 初始化商品列表
productList.add(new Product("商品1", 10.0, 100));
productList.add(new Product("商品2", 20.0, 200));
productList.add(new Product("商品3", 30.0, 300));
// 初始化RecyclerView
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ProductListAdapter(productList);
recyclerView.setAdapter(adapter);
}
private class ProductListAdapter extends RecyclerView.Adapter<ProductViewHolder> {
private List<Product> productList;
public ProductListAdapter(List<Product> productList) {
this.productList = productList;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
final 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.addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 添加到购物车
ShoppingCart.getInstance().addProduct(product);
Toast.makeText(ProductListActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
}
private static class ProductViewHolder extends RecyclerView.ViewHolder {
TextView nameTextView;
TextView priceTextView;
TextView quantityTextView;
Button addButton;
public ProductViewHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.nameTextView);
priceTextView = itemView.findViewById(R.id.priceTextView);
quantityTextView = itemView.findViewById(R.id.quantityTextView);
addButton = itemView.findViewById(R.id.addButton);
}
}
}
```
4. 购物车界面
```
public class ShoppingCartActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ShoppingCartAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
// 初始化RecyclerView
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ShoppingCartAdapter(ShoppingCart.getInstance().getProductList());
recyclerView.setAdapter(adapter);
// 显示总价
TextView totalPriceTextView = findViewById(R.id.totalPriceTextView);
double totalPrice = ShoppingCart.getInstance().getTotalPrice();
totalPriceTextView.setText(String.format("总价:%.2f元", totalPrice));
// 清空购物车
Button clearButton = findViewById(R.id.clearButton);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShoppingCart.getInstance().clear();
adapter.notifyDataSetChanged();
TextView totalPriceTextView = findViewById(R.id.totalPriceTextView);
totalPriceTextView.setText("总价:0.00元");
}
});
}
private class ShoppingCartAdapter extends RecyclerView.Adapter<ProductViewHolder> {
private List<Product> productList;
public ShoppingCartAdapter(List<Product> productList) {
this.productList = productList;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shopping_cart, parent, false);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
final 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.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 从购物车中删除
ShoppingCart.getInstance().removeProduct(product);
adapter.notifyDataSetChanged();
// 更新总价
TextView totalPriceTextView = findViewById(R.id.totalPriceTextView);
double totalPrice = ShoppingCart.getInstance().getTotalPrice();
totalPriceTextView.setText(String.format("总价:%.2f元", totalPrice));
}
});
holder.quantityEditText.setText(String.valueOf(product.getQuantity()));
holder.quantityEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (TextUtils.isEmpty(s)) {
return;
}
int quantity = Integer.parseInt(s.toString());
product.setQuantity(quantity);
// 更新总价
TextView totalPriceTextView = findViewById(R.id.totalPriceTextView);
double totalPrice = ShoppingCart.getInstance().getTotalPrice();
totalPriceTextView.setText(String.format("总价:%.2f元", totalPrice));
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
}
}
```
以上就是一个简单的购物车功能的实现,你可以根据自己的需求来进行扩展和优化。
阅读全文