From 291fc104cad3d5d29847ee049853b82b3d6391d4 Mon Sep 17 00:00:00 2001 From: pa3fjc76b <2505730605@qq.com> Date: Fri, 8 Oct 2021 00:57:05 +0800 Subject: [PATCH] ADD file via upload --- ListActivity.java | 103 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 ListActivity.java diff --git a/ListActivity.java b/ListActivity.java new file mode 100644 index 0000000..f259bd6 --- /dev/null +++ b/ListActivity.java @@ -0,0 +1,103 @@ +package com.example.health.foodlist; + +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.os.Bundle; +import android.text.TextUtils; +import android.view.View; +import android.widget.AdapterView; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.Toast; + +import com.example.health.Bean.FoodBean; +import com.example.health.Bean.Foodinfo; +import com.example.health.R; +import com.example.health.food_grid.FoodDescActivity; +import com.example.health.food_grid.FoodGridActivity; + +import java.util.ArrayList; +import java.util.List; + +public class ListActivity extends AppCompatActivity implements View.OnClickListener { + EditText list_edit; + ImageView list_refresh,list_serach; + ListView list_foodlist; +//Listview的内部数据源 + List mDatas; + List allFoodList; + private ListAdapter adapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_list); +//查找控件 + intiView(); +//找到Listview对应的数据源 + mDatas = new ArrayList<>(); + allFoodList = Foodinfo.getAllFoodList(); + mDatas.addAll(allFoodList); +//创建适配器 BaseAdapter的子类 + adapter = new ListAdapter(this, mDatas); + list_foodlist.setAdapter(adapter);//设置适配器 +//设置单向点击监听功能 + setListener(); + } + + private void setListener() { + list_foodlist.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + FoodBean foodBean = mDatas.get(position); + Intent intent = new Intent(ListActivity.this, FoodDescActivity.class); + intent.putExtra("food",foodBean); + startActivity(intent); + } + }); + } + + + private void intiView() { + list_edit=findViewById(R.id.info_edit); + list_refresh=findViewById(R.id.info_refresh); + list_serach=findViewById(R.id.info_search); + list_foodlist=findViewById(R.id.info_foodlistview); + list_refresh.setOnClickListener(this);//添加点击事件的监听器 + list_serach.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.info_refresh://刷新点击 + list_edit.setText(""); + mDatas.clear(); + mDatas.addAll(allFoodList); + adapter.notifyDataSetChanged(); + break; + + case R.id.info_search://搜索点击 +// 获取输入内容,判断不为空 + String input = list_edit.getText().toString().trim(); + if (TextUtils.isEmpty(input)) { + Toast.makeText(this, "输入内容不能为空!", Toast.LENGTH_SHORT).show(); + return; + } +// 判断所有食物列表的标题是否包含输入内容,如果包含,就添加到小集合中 + Listlist = new ArrayList<>(); + for (int i = 0; i < allFoodList.size(); i++) { + String foodName = allFoodList.get(i).getFoodName(); + if (foodName.contains(input)) { + list.add(allFoodList.get(i)); + } + mDatas.clear();//清空Listview的适配器的内容 + mDatas.addAll(list);//添加新的数据到数据源中 + adapter.notifyDataSetChanged();//提升适配器更新 + } + break; + } + } +}