安卓购物车功能的实现代码
时间: 2023-11-09 15:07:51 浏览: 64
实现一个安卓购物车功能的代码可以分为以下几个步骤:
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;
}
// getter and setter methods
}
```
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;
}
// getter and setter methods
}
```
3. 在界面中添加一个列表用于显示购物车中的商品,并提供添加商品和移除商品的按钮。
```
public class MainActivity extends AppCompatActivity {
private List<Product> productList = new ArrayList<>();
private ShoppingCart shoppingCart = new ShoppingCart();
private ProductAdapter productAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化商品列表
productList.add(new Product("商品1", 10.0, 1));
productList.add(new Product("商品2", 20.0, 1));
productList.add(new Product("商品3", 30.0, 1));
// 初始化购物车
shoppingCart.addProduct(productList.get(0));
// 设置商品列表适配器
productAdapter = new ProductAdapter(this, productList);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(productAdapter);
// 设置添加商品按钮的点击事件
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag();
Product product = productList.get(position);
shoppingCart.addProduct(product);
productAdapter.notifyDataSetChanged();
}
});
// 设置移除商品按钮的点击事件
Button removeButton = findViewById(R.id.remove_button);
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag();
Product product = productList.get(position);
shoppingCart.removeProduct(product);
productAdapter.notifyDataSetChanged();
}
});
}
// 商品列表适配器
private 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(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.product_item, parent, false);
}
Product product = productList.get(position);
TextView nameTextView = convertView.findViewById(R.id.name_text_view);
nameTextView.setText(product.getName());
TextView priceTextView = convertView.findViewById(R.id.price_text_view);
priceTextView.setText(String.format(Locale.getDefault(), "¥%.2f", product.getPrice()));
TextView quantityTextView = convertView.findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(product.getQuantity()));
Button addButton = convertView.findViewById(R.id.add_button);
addButton.setTag(position);
Button removeButton = convertView.findViewById(R.id.remove_button);
removeButton.setTag(position);
return convertView;
}
}
}
```
4. 在购物车界面中显示购物车中的商品和总价。
```
public class ShoppingCartActivity extends AppCompatActivity {
private ShoppingCart shoppingCart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
// 获取购物车对象
shoppingCart = ((MyApplication) getApplication()).getShoppingCart();
// 设置购物车列表适配器
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(new ProductAdapter(shoppingCart.getProductList()));
// 设置总价文本框
TextView totalPriceTextView = findViewById(R.id.total_price_text_view);
totalPriceTextView.setText(String.format(Locale.getDefault(), "总价:¥%.2f", shoppingCart.getTotalPrice()));
}
// 商品列表适配器
private class ProductAdapter extends BaseAdapter {
private List<Product> productList;
public ProductAdapter(List<Product> productList) {
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(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(ShoppingCartActivity.this).inflate(R.layout.product_item, parent, false);
}
Product product = productList.get(position);
TextView nameTextView = convertView.findViewById(R.id.name_text_view);
nameTextView.setText(product.getName());
TextView priceTextView = convertView.findViewById(R.id.price_text_view);
priceTextView.setText(String.format(Locale.getDefault(), "¥%.2f", product.getPrice()));
TextView quantityTextView = convertView.findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(product.getQuantity()));
return convertView;
}
}
}
```
阅读全文