用Android中的ListView写一个对美食的增删改查
时间: 2024-05-12 18:13:10 浏览: 125
好的,我们来一步步实现这个功能。首先,我们需要创建一个新的Android项目,然后在布局文件中添加ListView控件。接下来,我们需要定义一个美食类,包含美食的名称、图片、描述等属性。
```java
public class Food {
private String name;
private int imageId;
private String description;
public Food(String name, int imageId, String description) {
this.name = name;
this.imageId = imageId;
this.description = description;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
public String getDescription() {
return description;
}
}
```
接下来,我们需要创建一个适配器类,用于将美食数据显示在ListView中。
```java
public class FoodAdapter extends BaseAdapter {
private Context context;
private List<Food> foodList;
public FoodAdapter(Context context, List<Food> foodList) {
this.context = context;
this.foodList = foodList;
}
@Override
public int getCount() {
return foodList.size();
}
@Override
public Object getItem(int position) {
return foodList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_food, null);
holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.iv_food);
holder.tvName = convertView.findViewById(R.id.tv_name);
holder.tvDescription = convertView.findViewById(R.id.tv_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Food food = foodList.get(position);
holder.imageView.setImageResource(food.getImageId());
holder.tvName.setText(food.getName());
holder.tvDescription.setText(food.getDescription());
return convertView;
}
static class ViewHolder {
ImageView imageView;
TextView tvName;
TextView tvDescription;
}
}
```
我们还需要定义一个Activity类,用于处理ListView的增删改查操作。
```java
public class FoodActivity extends AppCompatActivity {
private List<Food> foodList = new ArrayList<>();
private FoodAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food);
ListView listView = findViewById(R.id.list_view);
mAdapter = new FoodAdapter(this, foodList);
listView.setAdapter(mAdapter);
// 添加按钮的点击事件
findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAddDialog();
}
});
}
/**
* 显示添加美食的对话框
*/
private void showAddDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("添加美食");
// 获取自定义的布局文件
View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_food, null);
final EditText etName = view.findViewById(R.id.et_name);
final EditText etDescription = view.findViewById(R.id.et_description);
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = etName.getText().toString().trim();
String description = etDescription.getText().toString().trim();
if (!TextUtils.isEmpty(name)) {
// 添加美食
Food food = new Food(name, R.drawable.food_default, description);
foodList.add(food);
mAdapter.notifyDataSetChanged();
}
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
}
```
这样,我们就实现了一个简单的美食增删改查功能。当然,你可以根据自己的需求对这个例子进行修改和扩展。
阅读全文