用Android Studio实现外卖订餐系统显示代码
时间: 2023-11-03 09:52:45 浏览: 134
抱歉,这个问题比较复杂,需要涉及到数据库、后端接口等多个方面的知识。以下是一个简单的示例代码,仅供参考:
MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<Dish> mDishList = new ArrayList<>();
private DishAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化 RecyclerView
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new DishAdapter(mDishList);
mRecyclerView.setAdapter(mAdapter);
// 加载菜品数据
loadDishes();
}
private void loadDishes() {
// 发送网络请求获取菜品列表
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://example.com/api/dishes";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
// 解析服务器返回的 JSON 数据
JSONArray jsonArray = response.optJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
Dish dish = new Dish();
dish.setName(jsonObject.optString("name"));
dish.setPrice(jsonObject.optDouble("price"));
dish.setDescription(jsonObject.optString("description"));
mDishList.add(dish);
}
// 刷新 RecyclerView
mAdapter.notifyDataSetChanged();
},
error -> Toast.makeText(this, "加载菜品失败", Toast.LENGTH_SHORT).show());
queue.add(request);
}
}
```
Dish.java
```
public class Dish {
private String name;
private double price;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
```
DishAdapter.java
```
public class DishAdapter extends RecyclerView.Adapter<DishAdapter.ViewHolder> {
private List<Dish> mDishList;
public DishAdapter(List<Dish> dishList) {
mDishList = dishList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_dish, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Dish dish = mDishList.get(position);
holder.nameTextView.setText(dish.getName());
holder.priceTextView.setText(String.format(Locale.getDefault(), "%.2f 元", dish.getPrice()));
holder.descriptionTextView.setText(dish.getDescription());
}
@Override
public int getItemCount() {
return mDishList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView nameTextView;
TextView priceTextView;
TextView descriptionTextView;
ViewHolder(View view) {
super(view);
nameTextView = view.findViewById(R.id.text_name);
priceTextView = view.findViewById(R.id.text_price);
descriptionTextView = view.findViewById(R.id.text_description);
}
}
}
```
item_dish.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/text_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="8dp" />
</LinearLayout>
```
此示例代码仅实现了菜品列表的显示,仍需后端接口和数据库支持才能实现完整的外卖订餐系统。
阅读全文