用android写一个汽车商城代码
时间: 2024-01-26 09:05:01 浏览: 133
好的,以下是一个简单的汽车商城代码示例,实现了浏览汽车、加入购物车和下订单的功能。请注意,这只是一个简单的示例,您需要根据自己的需求进行修改和完善。
1. 首先,创建一个包含汽车属性的Car类,如下所示:
```java
public class Car {
private String brand;
private String model;
private int price;
private String color;
private int image;
public Car(String brand, String model, int price, String color, int image) {
this.brand = brand;
this.model = model;
this.price = price;
this.color = color;
this.image = image;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public int getPrice() {
return price;
}
public String getColor() {
return color;
}
public int getImage() {
return image;
}
}
```
2. 创建一个汽车列表页面CarListActivity,在该页面中展示汽车列表,如下所示:
```java
public class CarListActivity extends AppCompatActivity {
private ArrayList<Car> carList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car_list);
carList = new ArrayList<>();
carList.add(new Car("Toyota", "Corolla", 20000, "Red", R.drawable.corolla));
carList.add(new Car("Honda", "Civic", 25000, "Blue", R.drawable.civic));
carList.add(new Car("Ford", "Mustang", 35000, "Yellow", R.drawable.mustang));
carList.add(new Car("BMW", "325i", 40000, "Black", R.drawable.bmw));
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
CarListAdapter adapter = new CarListAdapter(carList);
recyclerView.setAdapter(adapter);
}
}
```
在汽车列表页面中,我们使用RecyclerView展示汽车列表,并创建一个CarListAdapter作为RecyclerView的适配器,如下所示:
```java
public class CarListAdapter extends RecyclerView.Adapter<CarListAdapter.ViewHolder> {
private ArrayList<Car> carList;
public CarListAdapter(ArrayList<Car> carList) {
this.carList = carList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_car, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Car car = carList.get(position);
holder.brandTextView.setText(car.getBrand());
holder.modelTextView.setText(car.getModel());
holder.priceTextView.setText("$" + car.getPrice());
holder.colorTextView.setText(car.getColor());
holder.imageView.setImageResource(car.getImage());
holder.addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 将汽车加入购物车
}
});
}
@Override
public int getItemCount() {
return carList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView brandTextView;
public TextView modelTextView;
public TextView priceTextView;
public TextView colorTextView;
public ImageView imageView;
public Button addButton;
public ViewHolder(@NonNull View itemView) {
super(itemView);
brandTextView = itemView.findViewById(R.id.brandTextView);
modelTextView = itemView.findViewById(R.id.modelTextView);
priceTextView = itemView.findViewById(R.id.priceTextView);
colorTextView = itemView.findViewById(R.id.colorTextView);
imageView = itemView.findViewById(R.id.imageView);
addButton = itemView.findViewById(R.id.addButton);
}
}
}
```
在CarListAdapter中,我们使用了ViewHolder模式,将汽车的各个属性展示在item_car.xml中定义的布局中,并为“加入购物车”按钮设置点击事件。
3. 创建一个购物车页面CartActivity,在该页面中展示购物车中的商品列表,如下所示:
```java
public class CartActivity extends AppCompatActivity {
private ArrayList<Car> cartList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
cartList = new ArrayList<>();
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
CartListAdapter adapter = new CartListAdapter(cartList);
recyclerView.setAdapter(adapter);
}
}
```
在购物车页面中,我们同样使用RecyclerView展示购物车中的商品列表,并创建一个CartListAdapter作为RecyclerView的适配器,如下所示:
```java
public class CartListAdapter extends RecyclerView.Adapter<CartListAdapter.ViewHolder> {
private ArrayList<Car> cartList;
public CartListAdapter(ArrayList<Car> cartList) {
this.cartList = cartList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Car car = cartList.get(position);
holder.brandTextView.setText(car.getBrand());
holder.modelTextView.setText(car.getModel());
holder.priceTextView.setText("$" + car.getPrice());
holder.colorTextView.setText(car.getColor());
holder.imageView.setImageResource(car.getImage());
}
@Override
public int getItemCount() {
return cartList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView brandTextView;
public TextView modelTextView;
public TextView priceTextView;
public TextView colorTextView;
public ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
brandTextView = itemView.findViewById(R.id.brandTextView);
modelTextView = itemView.findViewById(R.id.modelTextView);
priceTextView = itemView.findViewById(R.id.priceTextView);
colorTextView = itemView.findViewById(R.id.colorTextView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
```
在CartListAdapter中,同样使用ViewHolder模式,将购物车中的商品展示在item_cart.xml中定义的布局中。
4. 创建一个订单页面OrderActivity,在该页面中展示订单详情,如下所示:
```java
public class OrderActivity extends AppCompatActivity {
private ArrayList<Car> orderList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
orderList = new ArrayList<>();
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
OrderListAdapter adapter = new OrderListAdapter(orderList);
recyclerView.setAdapter(adapter);
}
}
```
在订单页面中,同样使用RecyclerView展示订单详情中的商品列表,并创建一个OrderListAdapter作为RecyclerView的适配器,如下所示:
```java
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {
private ArrayList<Car> orderList;
public OrderListAdapter(ArrayList<Car> orderList) {
this.orderList = orderList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Car car = orderList.get(position);
holder.brandTextView.setText(car.getBrand());
holder.modelTextView.setText(car.getModel());
holder.priceTextView.setText("$" + car.getPrice());
holder.colorTextView.setText(car.getColor());
holder.imageView.setImageResource(car.getImage());
}
@Override
public int getItemCount() {
return orderList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView brandTextView;
public TextView modelTextView;
public TextView priceTextView;
public TextView colorTextView;
public ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
brandTextView = itemView.findViewById(R.id.brandTextView);
modelTextView = itemView.findViewById(R.id.modelTextView);
priceTextView = itemView.findViewById(R.id.priceTextView);
colorTextView = itemView.findViewById(R.id.colorTextView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
```
在OrderListAdapter中,同样使用ViewHolder模式,将订单详情中的商品展示在item_order.xml中定义的布局中。
5. 最后,在CarListActivity中为“加入购物车”按钮设置点击事件,并将选中的商品添加到购物车中:
```java
holder.addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Car car = carList.get(position);
cartList.add(car);
Toast.makeText(getApplicationContext(), "Added to cart", Toast.LENGTH_SHORT).show();
}
});
```
在购物车页面中,用户可以查看已选中的商品,并在订单页面中下订单。
以上是一个简单的汽车商城代码示例,您可以根据自己的需求进行修改和完善。
阅读全文