- 首先是UI设计,在layout文件夹下创建三个xml文件:activity_main.xml、menu_list_item.xml和order_list_item.xml,分别用于主界面、菜单列表和订单列表的展示。
activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择您要点的菜品"
android:textAlignment="center"
android:textSize="24sp" />
<ListView
android:id="@+id/menu_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null" />
<Button
android:id="@+id/btn_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="提交订单" />
<ListView
android:id="@+id/order_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="@null" />
</LinearLayout>
menu_list_item.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_menu_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_menu_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<Button
android:id="@+id/btn_add_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加入购物车" />
</LinearLayout>
order_list_item.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_order_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_order_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<Button
android:id="@+id/btn_delete_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
</LinearLayout>
2.接下来是后台代码的实现,在MainActivity.java中添加如下代码:
public class MainActivity extends AppCompatActivity {
private List<Menu> menuList = new ArrayList<>(); // 菜单列表
private List<Order> orderList = new ArrayList<>(); // 订单列表
private ListView lvMenu; // 菜单列表视图
private ListView lvOrder; // 订单列表视图
private Button btnOrder; // 提交订单按钮
private MenuAdapter menuAdapter; // 菜单列表适配器
private OrderAdapter orderAdapter; // 订单列表适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMenu(); // 初始化菜单数据
lvMenu = findViewById(R.id.menu_list);
lvOrder = findViewById(R.id.order_list);
btnOrder = findViewById(R.id.btn_order);
menuAdapter = new MenuAdapter(this, menuList);
orderAdapter = new OrderAdapter(this, orderList);
lvMenu.setAdapter(menuAdapter);
lvOrder.setAdapter(orderAdapter);
// 添加购物车操作
menuAdapter.setOnAddMenuListener(new MenuAdapter.OnAddMenuListener() {
@Override
public void onAddMenu(int position) {
Menu menu = menuList.get(position);
for (Order order : orderList) {
if (order.getName().equals(menu.getName())) { // 已有该菜品则添加数量
order.setCount(order.getCount() + 1);
orderAdapter.notifyDataSetChanged();
return;
}
}
Order order = new Order(menu.getName(), menu.getPrice(), 1);
orderList.add(order);
orderAdapter.notifyDataSetChanged();
}
});
// 删除订单操作
orderAdapter.setOnDeleteOrderListener(new OrderAdapter.OnDeleteOrderListener() {
@Override
public void onDeleteOrder(int position) {
orderList.remove(position);
orderAdapter.notifyDataSetChanged();
}
});
// 提交订单操作
btnOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 在这里实现订单提交和支付功能
Toast.makeText(MainActivity.this, "提交订单成功!", Toast.LENGTH_SHORT).show();
}
});
}
private void initMenu() {
menuList.add(new Menu("鱼香肉丝", 18));
menuList.add(new Menu("宫保鸡丁", 22));
menuList.add(new Menu("回锅肉", 20));
menuList.add(new Menu("水煮鱼", 30));
menuList.add(new Menu("毛血旺", 28));
}
}
其中,Menu类和Order类定义为:
public class Menu {
private String name; // 菜名
private int price; // 价格
public Menu(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
public class Order {
private String name; // 菜名
private int price; // 单价
private int count; // 数量
public Order(String name, int price, int count) {
this.name = name;
this.price = price;
this.count = count;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
并且定义了MenuAdapter和OrderAdapter作为菜单列表和订单列表的适配器:
public class MenuAdapter extends BaseAdapter {
private Context context;
private List<Menu> menuList;
private OnAddMenuListener onAddMenuListener;
public MenuAdapter(Context context, List<Menu> menuList) {
this.context = context;
this.menuList = menuList;
}
public void setOnAddMenuListener(OnAddMenuListener onAddMenuListener) {
this.onAddMenuListener = onAddMenuListener;
}
@Override
public int getCount() {
return menuList.size();
}
@Override
public Object getItem(int position) {
return menuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.menu_list_item, parent, false);
TextView tvName = view.findViewById(R.id.tv_menu_name);
TextView tvPrice = view.findViewById(R.id.tv_menu_price);
Button btnAdd = view.findViewById(R.id.btn_add_menu);
Menu menu = menuList.get(position);
tvName.setText(menu.getName());
tvPrice.setText(String.format(Locale.getDefault(), "%d元", menu.getPrice()));
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onAddMenuListener != null) {
onAddMenuListener.onAddMenu(position);
}
}
});
return view;
}
public interface OnAddMenuListener {
void onAddMenu(int position);
}
}
public class OrderAdapter extends BaseAdapter {
private Context context;
private List<Order> orderList;
private OnDeleteOrderListener onDeleteOrderListener;
public OrderAdapter(Context context, List<Order> orderList) {
this.context = context;
this.orderList = orderList;
}
public void setOnDeleteOrderListener(OnDeleteOrderListener onDeleteOrderListener) {
this.onDeleteOrderListener = onDeleteOrderListener;
}
@Override
public int getCount() {
return orderList.size();
}
@Override
public Object getItem(int position) {
return orderList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.order_list_item, parent, false);
TextView tvName = view.findViewById(R.id.tv_order_name);
TextView tvPrice = view.findViewById(R.id.tv_order_price);
Button btnDelete = view.findViewById(R.id.btn_delete_order);
Order order = orderList.get(position);
tvName.setText(order.getName());
tvPrice.setText(String.format(Locale.getDefault(), "%d元 x %d", order.getPrice(), order.getCount()));
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onDeleteOrderListener != null) {
onDeleteOrderListener.onDeleteOrder(position);
}
}
});
return view;
}
public interface OnDeleteOrderListener {
void onDeleteOrder(int position);
}
}
3.最后是支付功能的实现,在btnOrder.setOnClickListener中添加如下代码:
btnOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (orderList.isEmpty()) {
Toast.makeText(MainActivity.this, "请添加菜品到购物车", Toast.LENGTH_SHORT).show();
return;
}
int totalPrice = 0;
for (Order order : orderList) {
totalPrice += order.getPrice() * order.getCount();
}
String message = "订单总价:" + totalPrice + "元";
new AlertDialog.Builder(MainActivity.this)
.setTitle("确认订单")
.setMessage(message)
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 在这里实现支付功能
Toast.makeText(MainActivity.this, "支付成功!", Toast.LENGTH_SHORT).show();
orderList.clear();
orderAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("取消", null)
.show();
}
});
以上是一个简单的订餐管理的实现示例,您可以根据自己的需求进行修改和完善。