用android实现购物车功能,它可以可实现商品数量的增减、商品的删除以及总价格计算功能
时间: 2024-02-05 20:13:11 浏览: 139
实现购物车功能可以分为以下几个步骤:
1. 创建一个商品类,包含商品的名称、价格、图片等信息。
```
public class Product {
private String name;
private double price;
private int image;
public Product(String name, double price, int image) {
this.name = name;
this.price = price;
this.image = image;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getImage() {
return image;
}
}
```
2. 创建一个购物车类,包含商品列表和总价格。
```
public class ShoppingCart {
private List<Product> productList;
private double totalPrice;
public ShoppingCart() {
productList = new ArrayList<>();
totalPrice = 0;
}
public List<Product> getProductList() {
return productList;
}
public void setProductList(List<Product> productList) {
this.productList = productList;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public void addProduct(Product product) {
productList.add(product);
totalPrice += product.getPrice();
}
public void removeProduct(Product product) {
productList.remove(product);
totalPrice -= product.getPrice();
}
}
```
3. 在界面中展示商品列表,并添加增减和删除按钮。
```
public class MainActivity extends AppCompatActivity {
private ListView listView;
private TextView totalPriceTextView;
private ShoppingCart shoppingCart;
private ProductAdapter productAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
totalPriceTextView = findViewById(R.id.total_price_text_view);
shoppingCart = new ShoppingCart();
productAdapter = new ProductAdapter(this, shoppingCart.getProductList());
listView.setAdapter(productAdapter);
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Product product = new Product("商品", 10.0, R.drawable.product);
shoppingCart.addProduct(product);
productAdapter.notifyDataSetChanged();
totalPriceTextView.setText("总价:" + shoppingCart.getTotalPrice());
}
});
Button removeButton = findViewById(R.id.remove_button);
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!shoppingCart.getProductList().isEmpty()) {
Product product = shoppingCart.getProductList().get(0);
shoppingCart.removeProduct(product);
productAdapter.notifyDataSetChanged();
totalPriceTextView.setText("总价:" + shoppingCart.getTotalPrice());
}
}
});
}
}
```
4. 创建一个商品适配器类,用于展示商品列表。在适配器中实现商品数量的增减和商品的删除功能。
```
public class ProductAdapter extends BaseAdapter {
private Context context;
private List<Product> productList;
public ProductAdapter(Context context, List<Product> productList) {
this.context = context;
this.productList = productList;
}
@Override
public int getCount() {
return productList.size();
}
@Override
public Object getItem(int position) {
return productList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.product_item, parent, false);
}
ImageView imageView = convertView.findViewById(R.id.image_view);
TextView nameTextView = convertView.findViewById(R.id.name_text_view);
TextView priceTextView = convertView.findViewById(R.id.price_text_view);
ImageButton addButton = convertView.findViewById(R.id.add_button);
final TextView countTextView = convertView.findViewById(R.id.count_text_view);
ImageButton removeButton = convertView.findViewById(R.id.remove_button);
final Product product = productList.get(position);
imageView.setImageResource(product.getImage());
nameTextView.setText(product.getName());
priceTextView.setText("价格:" + product.getPrice());
countTextView.setText("数量:1");
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = Integer.parseInt(countTextView.getText().toString()) + 1;
countTextView.setText("数量:" + count);
shoppingCart.setTotalPrice(shoppingCart.getTotalPrice() + product.getPrice());
notifyDataSetChanged();
totalPriceTextView.setText("总价:" + shoppingCart.getTotalPrice());
}
});
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = Integer.parseInt(countTextView.getText().toString()) - 1;
if (count < 1) {
shoppingCart.removeProduct(product);
productList.remove(product);
} else {
countTextView.setText("数量:" + count);
shoppingCart.setTotalPrice(shoppingCart.getTotalPrice() - product.getPrice());
}
notifyDataSetChanged();
totalPriceTextView.setText("总价:" + shoppingCart.getTotalPrice());
}
});
return convertView;
}
}
```
以上就是一个简单的购物车功能实现的示例。需要注意的是,实际项目中可能还需要处理一些其他的逻辑,例如商品库存不足时的提示等。
阅读全文