Android 实现购物功能
时间: 2023-12-21 12:22:41 浏览: 78
Android Studio实现功能强大的购物商城APP,优秀课设,高分必看
5星 · 资源好评率100%
实现购物功能需要考虑以下几个方面:
1. 商品展示:需要在应用中展示商品列表,可以使用 RecyclerView 或 ListView 等控件来展示商品信息,包括商品名称、价格、图片等。
2. 购物车:用户可以将商品添加到购物车中,可以使用 SQLite 数据库或 SharedPreferences 存储购物车中的商品信息。
3. 结算:用户可以对购物车中的商品进行结算,需要将购物车中的商品信息传递给结算页面,计算总价并提供支付功能。
4. 订单管理:用户可以查看历史订单和订单详情。
下面是一个简单的示例代码:
1. 商品展示
```
// 商品列表页面
public class ProductListActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ProductListAdapter adapter;
private List<Product> productList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
recyclerView = findViewById(R.id.recycler_view);
productList = getProductList(); // 获取商品列表数据
adapter = new ProductListAdapter(this, productList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
private List<Product> getProductList() {
// 获取商品列表数据
}
}
// 商品列表适配器
public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {
private Context context;
private List<Product> productList;
public ProductListAdapter(Context context, List<Product> productList) {
this.context = context;
this.productList = productList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).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.name.setText(product.getName());
holder.price.setText(String.valueOf(product.getPrice()));
// 加载商品图片
Glide.with(context).load(product.getImageUrl()).into(holder.image);
}
@Override
public int getItemCount() {
return productList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView price;
ImageView image;
ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
price = itemView.findViewById(R.id.price);
image = itemView.findViewById(R.id.image);
}
}
}
```
2. 购物车
```
// 商品详情页面
public class ProductDetailActivity extends AppCompatActivity {
private Product product;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
product = getProduct(); // 获取商品详情数据
// 显示商品详情信息
TextView name = findViewById(R.id.name);
name.setText(product.getName());
TextView price = findViewById(R.id.price);
price.setText(String.valueOf(product.getPrice()));
ImageView image = findViewById(R.id.image);
Glide.with(this).load(product.getImageUrl()).into(image);
// 添加到购物车
Button addToCartBtn = findViewById(R.id.add_to_cart_btn);
addToCartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addToCart(product);
}
});
}
private Product getProduct() {
// 获取商品详情数据
}
private void addToCart(Product product) {
List<Product> cartList = getCartList(); // 获取购物车列表
boolean hasProduct = false;
for (Product p : cartList) {
if (p.getId() == product.getId()) {
// 购物车中已存在该商品,数量加一
p.setQuantity(p.getQuantity() + 1);
hasProduct = true;
break;
}
}
if (!hasProduct) {
// 购物车中不存在该商品,添加到购物车
product.setQuantity(1);
cartList.add(product);
}
saveCartList(cartList); // 保存购物车列表
Toast.makeText(this, "已添加到购物车", Toast.LENGTH_SHORT).show();
}
private List<Product> getCartList() {
// 从 SharedPreferences 或 SQLite 中获取购物车列表
}
private void saveCartList(List<Product> cartList) {
// 将购物车列表保存到 SharedPreferences 或 SQLite 中
}
}
```
3. 结算
```
// 购物车页面
public class ShoppingCartActivity extends AppCompatActivity {
private List<Product> cartList;
private ShoppingCartAdapter adapter;
private TextView totalPriceText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
cartList = getCartList(); // 获取购物车列表
adapter = new ShoppingCartAdapter(this, cartList);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
// 显示总价
double totalPrice = getTotalPrice(cartList);
totalPriceText = findViewById(R.id.total_price);
totalPriceText.setText(String.valueOf(totalPrice));
// 结算
Button checkoutBtn = findViewById(R.id.checkout_btn);
checkoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkout(cartList);
}
});
}
private List<Product> getCartList() {
// 从 SharedPreferences 或 SQLite 中获取购物车列表
}
private double getTotalPrice(List<Product> cartList) {
double totalPrice = 0;
for (Product product : cartList) {
totalPrice += product.getPrice() * product.getQuantity();
}
return totalPrice;
}
private void checkout(List<Product> cartList) {
// 将购物车列表传递给结算页面
Intent intent = new Intent(this, CheckoutActivity.class);
intent.putExtra("cartList", (Serializable) cartList);
startActivity(intent);
}
}
// 购物车适配器
public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.ViewHolder> {
private Context context;
private List<Product> cartList;
public ShoppingCartAdapter(Context context, List<Product> cartList) {
this.context = context;
this.cartList = cartList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_shopping_cart, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Product product = cartList.get(position);
holder.name.setText(product.getName());
holder.price.setText(String.valueOf(product.getPrice()));
holder.quantity.setText(String.valueOf(product.getQuantity()));
// 加载商品图片
Glide.with(context).load(product.getImageUrl()).into(holder.image);
}
@Override
public int getItemCount() {
return cartList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView price;
TextView quantity;
ImageView image;
ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
price = itemView.findViewById(R.id.price);
quantity = itemView.findViewById(R.id.quantity);
image = itemView.findViewById(R.id.image);
}
}
}
// 结算页面
public class CheckoutActivity extends AppCompatActivity {
private List<Product> cartList;
private TextView totalPriceText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
cartList = (List<Product>) getIntent().getSerializableExtra("cartList"); // 获取购物车列表
totalPriceText = findViewById(R.id.total_price);
double totalPrice = getTotalPrice(cartList);
totalPriceText.setText(String.valueOf(totalPrice));
// 支付
Button payBtn = findViewById(R.id.pay_btn);
payBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pay(totalPrice);
}
});
}
private double getTotalPrice(List<Product> cartList) {
double totalPrice = 0;
for (Product product : cartList) {
totalPrice += product.getPrice() * product.getQuantity();
}
return totalPrice;
}
private void pay(double totalPrice) {
// 调用支付接口
Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
saveOrder(); // 保存订单信息
finish();
}
private void saveOrder() {
// 将购物车列表转换为订单信息,并保存到 SQLite 中
}
}
```
4. 订单管理
```
// 历史订单页面
public class OrderHistoryActivity extends AppCompatActivity {
private List<Order> orderList;
private OrderListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_history);
orderList = getOrderList(); // 获取历史订单列表
adapter = new OrderListAdapter(this, orderList);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
private List<Order> getOrderList() {
// 从 SQLite 中获取历史订单列表
}
}
// 历史订单适配器
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {
private Context context;
private List<Order> orderList;
public OrderListAdapter(Context context, List<Order> orderList) {
this.context = context;
this.orderList = orderList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_order, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Order order = orderList.get(position);
holder.orderId.setText(order.getOrderId());
holder.orderTime.setText(order.getOrderTime());
holder.totalPrice.setText(String.valueOf(order.getTotalPrice()));
// 加载商品图片
Glide.with(context).load(order.getProductList().get(0).getImageUrl()).into(holder.image);
}
@Override
public int getItemCount() {
return orderList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView orderId;
TextView orderTime;
TextView totalPrice;
ImageView image;
ViewHolder(View itemView) {
super(itemView);
orderId = itemView.findViewById(R.id.order_id);
orderTime = itemView.findViewById(R.id.order_time);
totalPrice = itemView.findViewById(R.id.total_price);
image = itemView.findViewById(R.id.image);
}
}
}
// 订单详情页面
public class OrderDetailActivity extends AppCompatActivity {
private Order order;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_detail);
order = getOrder(); // 获取订单详情
// 显示订单详情信息
TextView orderId = findViewById(R.id.order_id);
orderId.setText(order.getOrderId());
TextView orderTime = findViewById(R.id.order_time);
orderTime.setText(order.getOrderTime());
TextView totalPrice = findViewById(R.id.total_price);
totalPrice.setText(String.valueOf(order.getTotalPrice()));
ImageView image = findViewById(R.id.image);
Glide.with(this).load(order.getProductList().get(0).getImageUrl()).into(image);
}
private Order getOrder() {
// 从 SQLite 中获取订单详情
}
}
```
以上是一个简单的购物功能示例,具体实现方式可能有所不同,可以根据自己的需求进行调整。
阅读全文