android编辑购物车,Android制作简单的普通购物车
时间: 2023-11-27 09:53:48 浏览: 73
Android制作简单的普通购物车
要制作一个简单的购物车功能,需要以下步骤:
1. 创建一个商品列表:在你的 Android 应用中创建一个列表,用于显示所有可购买的商品。
2. 添加购物车按钮:在每个商品项目上添加一个“加入购物车”按钮。
3. 创建购物车列表:创建一个新的列表,用于显示所有已添加到购物车中的商品。
4. 处理添加到购物车事件:当用户点击“加入购物车”按钮时,将所选商品添加到购物车列表中。
5. 显示购物车总价:在购物车列表底部添加一个元素,用于显示所有商品的总价。
以下是一个简单的示例代码,可以帮助你开始编写购物车功能:
1. 创建商品列表
```
public class MainActivity extends AppCompatActivity {
private List<Product> productList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 添加商品
productList.add(new Product("商品1", 10));
productList.add(new Product("商品2", 20));
productList.add(new Product("商品3", 30));
// 设置商品列表
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new ProductAdapter(productList));
}
}
```
2. 添加购物车按钮
```
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.product_item, 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.addToCartButton.setOnClickListener(view -> {
// 处理添加到购物车事件
});
}
@Override
public int getItemCount() {
return productList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public TextView priceTextView;
public Button addToCartButton;
public ViewHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.nameTextView);
priceTextView = itemView.findViewById(R.id.priceTextView);
addToCartButton = itemView.findViewById(R.id.addToCartButton);
}
}
}
```
3. 创建购物车列表
```
public class CartActivity extends AppCompatActivity {
private List<Product> cartList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
// 设置购物车列表
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new CartAdapter(cartList));
}
}
```
4. 处理添加到购物车事件
```
public class MainActivity extends AppCompatActivity {
private List<Product> productList = new ArrayList<>();
private List<Product> cartList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 添加商品
productList.add(new Product("商品1", 10));
productList.add(new Product("商品2", 20));
productList.add(new Product("商品3", 30));
// 设置商品列表
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new ProductAdapter(productList, this::addToCart));
}
private void addToCart(Product product) {
cartList.add(product);
// 更新购物车列表
RecyclerView recyclerView = findViewById(R.id.cartRecyclerView);
CartAdapter adapter = (CartAdapter) recyclerView.getAdapter();
adapter.notifyDataSetChanged();
}
}
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private List<Product> productList;
private OnAddToCartClickListener clickListener;
public ProductAdapter(List<Product> productList, OnAddToCartClickListener clickListener) {
this.productList = productList;
this.clickListener = clickListener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_item, 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.addToCartButton.setOnClickListener(view -> {
clickListener.onAddToCartClick(product);
});
}
@Override
public int getItemCount() {
return productList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public TextView priceTextView;
public Button addToCartButton;
public ViewHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.nameTextView);
priceTextView = itemView.findViewById(R.id.priceTextView);
addToCartButton = itemView.findViewById(R.id.addToCartButton);
}
}
public interface OnAddToCartClickListener {
void onAddToCartClick(Product product);
}
}
```
5. 显示购物车总价
```
public class CartActivity extends AppCompatActivity {
private List<Product> cartList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
// 设置购物车列表
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new CartAdapter(cartList));
// 设置购物车总价
TextView totalPriceTextView = findViewById(R.id.totalPriceTextView);
int totalPrice = 0;
for (Product product : cartList) {
totalPrice += product.getPrice();
}
totalPriceTextView.setText(String.valueOf(totalPrice));
}
}
```
希望这个例子能帮助你开始制作购物车功能。
阅读全文