FoodUtils.java
package com.example.healthydiet;
import java.util.ArrayList;
import java.util.List;
public class FoodUtils {
private static final String[] food = {
"猪肉","猪肝","牛肉","猪肝","牛肉","猪肝","牛肉","猪肝",
"猪肝","牛肉","猪肝","牛肉","猪肝","牛肉","猪肝","牛肉",
"猪肝","牛肉","猪肝","牛肉","猪肝","zhu","猪肝","牛肉",
"猪肝","牛肉","猪肝","牛肉","猪肝","zhu"};
private static final String[] food1 ={
"黄瓜","芥末","黄瓜","芥末","黄瓜","芥末","黄瓜","芥末",
"黄瓜","芥末","黄瓜","芥末","黄瓜","芥末","黄瓜","芥末",
"黄瓜","芥末","黄瓜","芥末","黄瓜","芥末","黄瓜","芥末",
"黄瓜","芥末","黄瓜","芥末","黄瓜","芥末"};
private static final int[] resId = {
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
private static final String[] foodjianjie = {
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一",
"猪肉是目前人们餐桌上最常见的肉类食物之一","猪肉是目前人们餐桌上最常见的肉类食物之一"};
public static List<FoodBean> getAilFoodList() {
List<FoodBean> list =new ArrayList<>();
for (int i = 0; i< food.length;i++) {
FoodBean bean = new FoodBean(food[i],food1[i],foodjianjie[i],resId[i] );
list.add(bean);
}
return list;
}
}
FoodBean.java
package com.example.healthydiet;
public class FoodBean {
private String title;
private String notmatch;
private String dasc;
private int picId;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNotmatch() {
return notmatch;
}
public void setNotmatch(String notmatch) {
this.notmatch = notmatch;
}
public String getDasc() {
return dasc;
}
public void setDasc(String dasc) {
this.dasc = dasc;
}
public int getPicId() {
return picId;
}
public void setPicId(int picId) {
this.picId = picId;
}
public FoodBean(String title, String notmatch, String dasc, int picId) {
this.title = title;
this.notmatch = notmatch;
this.dasc = dasc;
this.picId = picId;
}
}
infoListAdatpter.java
package com.example.healthydiet;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class infoListAdatpter extends BaseAdapter {
Context context;
List<FoodBean> mDatas;
public infoListAdatpter(Context context, List<FoodBean> mDatas) {
this.context = context;
this.mDatas = mDatas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_infolist_lv,null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
FoodBean foodBean = mDatas.get(position);
holder.titleTv.setText(foodBean.getTitle());
holder.notTv.setText("不可匹配"+foodBean.getNotmatch());
holder.iv.setImageResource(foodBean.getPicId());
return convertView;
}
class ViewHolder{
ImageView iv;
TextView titleTv,notTv;
public ViewHolder(View view) {
iv = view.findViewById(R.id.item_info_iv);
titleTv = view.findViewById(R.id.itme_info_tv_title);
notTv = view.findViewById(R.id.item_info_tv_notmatch);
}
}
}
item_infolist_lv.xml
<?xml version="1.0" encoding="utf-8"?>
<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/item_info_iv"
android:layout_width="120dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher"
android:scaleType="fitXY"
/>
<TextView
android:id="@+id/itme_info_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/item_info_iv"
android:layout_marginTop="10dp"
android:text="牛奶"
android:layout_marginLeft="10dp"
android:textSize="20sp"
/>
<TextView
android:id="@+id/item_info_tv_notmatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/itme_info_tv_title"
android:layout_alignBottom="@+id/item_info_iv"
android:text="钢筋"
/>
<ImageView
android:id="@+id/itme_info_iv_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
activity_info_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".infoListActivity">
<RelativeLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/purple_200">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/info_flush"
android:src="@mipmap/ic_launcher"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
<ImageView
android:id="@+id/info_iv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_toLeftOf="@+id/info_flush"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
/>
<EditText
android:id="@+id/info_et_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:layout_toLeftOf="@+id/info_iv_search"
android:textColor="@color/white"
android:background="@drawable/info_et_bg"
/>
</RelativeLayout>
<ListView
android:id="@+id/info_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/dividercolor"
android:dividerHeight="1dp"
>
</ListView>
</LinearLayout>
infoListActivity.java
package com.example.healthydiet;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class infoListActivity extends AppCompatActivity implements View.OnClickListener{
EditText searchEt;
ImageView searchLv,flusIv;
ListView showLw;
List<FoodBean>mDatas;
List<FoodBean>allFoodList;
private infoListAdatpter adpter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_list);
initView();
mDatas = new ArrayList<>();
allFoodList = FoodUtils.getAilFoodList();
mDatas.addAll(allFoodList);
adpter = new infoListAdatpter(this,mDatas);
showLw.setAdapter(adpter);
}
private void initView() {
searchEt = findViewById(R.id.info_et_search);
searchLv = findViewById(R.id.info_iv_search);
flusIv = findViewById(R.id.info_flush);
showLw = findViewById(R.id.info_lv);
searchLv.setOnClickListener(this);
flusIv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.info_flush://刷新
mDatas.clear();
mDatas.addAll(allFoodList);
adpter.notifyDataSetChanged();
break;
case R.id.info_iv_search://搜索
String msg = searchEt.getText().toString().trim();
if (TextUtils.isEmpty(msg)) {
Toast.makeText(this,"输入内容不能为空",Toast.LENGTH_LONG).show();
return;
}
List<FoodBean> list = new ArrayList<>();
for (int i = 0;i <allFoodList.size();i++) {
String title = allFoodList.get(i).getTitle();
if (title.contains(msg)) {
list.add(allFoodList.get(i));
}
}
mDatas.clear();
mDatas.addAll(list);
adpter.notifyDataSetChanged();
break;
default:
break;
}
}
}
MainActivity.java
package com.example.healthydiet;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
Intent intent = new Intent();
switch (view.getId()){
case R.id.home_btn1:
intent.setClass(MainActivity.this,infoListActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
info_et_bg.xml 这个文件放drawable文件下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp"></corners>
<stroke android:width="2dp" android:color="@color/white"></stroke>
</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/home_btn1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@color/black"
android:layout_marginTop="180dp"
android:onClick="onClick"
/>
</LinearLayout>
|