在Android studio,用BaseAdapter写一个菜单订餐系统,界面包含各菜品照片、菜名、介绍、价格、数量增减按钮、各菜品总价 ,及确认按钮的代码
时间: 2023-10-20 08:33:39 浏览: 74
好的,下面是一个简单的示例代码来实现这个菜单订餐系统。请注意,这只是一个示例,你需要根据自己的需求进行修改和完善。
布局文件menu_item.xml:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/menu_item_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/menu_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/menu_item_image"
android:layout_marginStart="10dp"
android:text="菜名"
android:textSize="18sp" />
<TextView
android:id="@+id/menu_item_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/menu_item_name"
android:layout_toEndOf="@+id/menu_item_image"
android:layout_marginStart="10dp"
android:text="介绍"
android:textSize="14sp" />
<TextView
android:id="@+id/menu_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="价格"
android:textSize="18sp" />
<Button
android:id="@+id/menu_item_minus"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignBottom="@+id/menu_item_plus"
android:layout_toStartOf="@+id/menu_item_plus"
android:text="-" />
<Button
android:id="@+id/menu_item_plus"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@+id/menu_item_desc"
android:layout_alignParentEnd="true"
android:text="+" />
<TextView
android:id="@+id/menu_item_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/menu_item_desc"
android:layout_alignParentEnd="true"
android:text="总价"
android:textSize="14sp" />
</RelativeLayout>
```
Java类MenuAdapter.java:
```
public class MenuAdapter extends BaseAdapter {
private List<MenuItem> menuItems;
private LayoutInflater inflater;
public MenuAdapter(Context context, List<MenuItem> menuItems) {
this.menuItems = menuItems;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return menuItems.size();
}
@Override
public MenuItem getItem(int position) {
return menuItems.get(position);
}
@Override
public long getItemId(int position) {
return menuItems.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();
holder.image = convertView.findViewById(R.id.menu_item_image);
holder.name = convertView.findViewById(R.id.menu_item_name);
holder.desc = convertView.findViewById(R.id.menu_item_desc);
holder.price = convertView.findViewById(R.id.menu_item_price);
holder.minus = convertView.findViewById(R.id.menu_item_minus);
holder.plus = convertView.findViewById(R.id.menu_item_plus);
holder.totalPrice = convertView.findViewById(R.id.menu_item_total_price);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final MenuItem menuItem = getItem(position);
holder.image.setImageResource(menuItem.getImageRes());
holder.name.setText(menuItem.getName());
holder.desc.setText(menuItem.getDesc());
holder.price.setText(String.format(Locale.getDefault(), "¥%d", menuItem.getPrice()));
holder.totalPrice.setText(String.format(Locale.getDefault(), "¥%d", menuItem.getTotalPrice()));
holder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = menuItem.getCount();
if (count > 0) {
menuItem.setCount(count - 1);
menuItem.setTotalPrice(menuItem.getPrice() * menuItem.getCount());
notifyDataSetChanged();
}
}
});
holder.plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = menuItem.getCount();
menuItem.setCount(count + 1);
menuItem.setTotalPrice(menuItem.getPrice() * menuItem.getCount());
notifyDataSetChanged();
}
});
return convertView;
}
private static class ViewHolder {
ImageView image;
TextView name;
TextView desc;
TextView price;
Button minus;
Button plus;
TextView totalPrice;
}
}
```
Java类MenuItem.java:
```
public class MenuItem {
private int id;
private int imageRes;
private String name;
private String desc;
private int price;
private int count;
private int totalPrice;
public MenuItem(int id, int imageRes, String name, String desc, int price) {
this.id = id;
this.imageRes = imageRes;
this.name = name;
this.desc = desc;
this.price = price;
this.count = 0;
this.totalPrice = 0;
}
public int getId() {
return id;
}
public int getImageRes() {
return imageRes;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
public int getPrice() {
return price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(int totalPrice) {
this.totalPrice = totalPrice;
}
}
```
最后,在MainActivity中创建菜单数据并设置Adapter:
```
public class MainActivity extends AppCompatActivity {
private ListView menuListView;
private List<MenuItem> menuItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menuListView = findViewById(R.id.menu_list_view);
// 创建菜单数据
menuItems = new ArrayList<>();
menuItems.add(new MenuItem(1, R.drawable.menu_item_1, "菜品1", "这是菜品1的介绍", 10));
menuItems.add(new MenuItem(2, R.drawable.menu_item_2, "菜品2", "这是菜品2的介绍", 15));
menuItems.add(new MenuItem(3, R.drawable.menu_item_3, "菜品3", "这是菜品3的介绍", 20));
// 设置Adapter
MenuAdapter menuAdapter = new MenuAdapter(this, menuItems);
menuListView.setAdapter(menuAdapter);
}
}
```
这样,一个简单的菜单订餐系统就完成了。当然,你可以根据自己的需求进行修改和扩展。
阅读全文