健康饮食App

摘要:

随着时代的发展,人们对饮食的看法从色香味到注重食品的健康问题,人们一直在追寻一种好吃吃好的健康理念,但是人们对怎样饮食才健康知之甚少。怎样才能吃得健康,国内外对于食品健康的不同状况,哪些是健康的饮食习惯等,都是人们很关心的问题。

关键词:

饮食;健康。

前言:

近年来,随着科学水平的飞速发展,人民生活水平的提高。人们对饮食的要求已经不再局限于吃饱、吃好,更要在饮食中得到健康。

正文:

“安生之本,必资于食,不知宜食者,不足以生存也”

以此我们可以看出吃什么以及如何吃都是时刻的关系到我们的身体健康,甚至是关乎人的生存质量。健康饮食的要点就是:合理偏食为健康充电,缺啥补啥;少食肉类食品和动物性脂肪,多吃植物性食物和新鲜蔬菜及水果,注意蛋白质摄取均衡,减少不良的饮食习惯。还可以根据不同的年龄段选择不同的食物。古人尚且如此看重吃的作用,那么在这个物质丰富的年代,我们就应该吃得好,吃得健康,吃得放心。  

接下来我将用android studio编写健康饮食app为大家详细介绍介绍。

注:项目源码在最后

学  号   231        

姓  名      福福          

指导教师:岳汶华/张毅恒

 2022  6  13

实训题目:健康饮食App的开发与应用

 

1  项目概述总览图

扫描二维码关注公众号,回复: 15257221 查看本文章

2  任务题目与思路步骤截图

2.1 任务一:创建HomeMenuActivity和InfoListActivity活动页面

2.1.1 步骤任务

(1)打开android studio编译软件

  1. 新建项目

2.1选择File--->New--->New Project--->下一步

2.2选择Phone and Tablet--->Empty Activity--->下一步

2.3填好项目名、包名、保存路径、语言等信息,点击完成

  1. 导入实训图片资源

3.1将图片复制,粘贴到项目res包下的 mipmap包的hdpi包中

(4)新建HomeMenuActivity活动页面

4.1在cn.edu.guit.health_231后三位包下鼠标右键新建一个空白activity

4.2 activity名字为HomeMenuActivity

package cn.edu.guit.health_231;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import cn.edu.guit.health_231.Slither.SlitherActivity;
import cn.edu.guit.health_231.about_me.AboutMeActivity;
import cn.edu.guit.health_231.food_grid.FoodGridActivity;
import cn.edu.guit.health_231.food_list.InfoListActivity;

public class HomeMenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_menu);
    }

    public void onClick(View view){
        Intent intent = new Intent();
        switch (view.getId()){
            case R.id.home_bnt1:
                intent.setClass(HomeMenuActivity.this, InfoListActivity.class);
                break;
            case R.id.home_bnt2:
                intent.setClass(HomeMenuActivity.this, FoodGridActivity.class);
                break;
            case R.id.home_bnt3:
                intent.setClass(HomeMenuActivity.this, AboutMeActivity.class);
                break;
            case R.id.home_bnt4:
                intent.setClass(HomeMenuActivity.this, SlitherActivity.class);
                break;
        }
        startActivity(intent);
    }
}

4.3完善编辑好res--->laout--->activity_home_menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@mipmap/home_page">
    <Button
        android:id="@+id/home_bnt1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@mipmap/btn_food_list"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="180dp"
        android:onClick="onClick">

    </Button>

    <Button
        android:id="@+id/home_bnt2"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@mipmap/btn_food_nutriction"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40dp"
        android:onClick="onClick"
        ></Button>

    <Button
        android:id="@+id/home_bnt3"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@mipmap/btn_food_about"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40dp"
        android:onClick="onClick"
        ></Button>

    <Button
        android:id="@+id/home_bnt4"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text="校园风光"
        android:textSize="32dp"
        android:textColor="@color/white"
        android:background="@mipmap/search_background"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:onClick="onClick"
        ></Button>

</LinearLayout>

2.1.2 任务步骤

(1)创建InfoListActivity活动页面

1.1 在cn.edu.guit.health_231包下,鼠标右键新建一个空白activity

1.2 activity名字为InfoListActivity

1.3最终效果

  

package cn.edu.guit.health_231.food_list;

import android.annotation.SuppressLint;
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 androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import cn.edu.guit.health_231.R;
import cn.edu.guit.health_231.bean.FoodBean;
import cn.edu.guit.health_231.food_desc.FoodDescActivity;
import cn.edu.guit.health_231.sql.MySQLiteOpenHelper;


public class InfoListActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener {
    EditText searchEt;
    ImageView searchIv, flushIv;
    ListView showLv;
    List<FoodBean> mDatas = new ArrayList<>();   //视图要显示的数据集合
    List<FoodBean> allFoodList = new ArrayList<>();//从数据库查询出所有的数据
    InfoListAdapter adapter = null;
    MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_list);
        initView();
        mySQLiteOpenHelper.initDate();
        allFoodList = mySQLiteOpenHelper.findAll();
        mDatas.addAll(allFoodList);
        adapter = new InfoListAdapter(this, mDatas);
        showLv.setAdapter(adapter);
        setListener();
    }

    @SuppressLint("WrongViewCast")
    protected void initView() {
        //控件的声明info_et_searched
        searchEt = findViewById(R.id.info_et_searched);
        flushIv = findViewById(R.id.info_iv_flush);
        showLv = findViewById(R.id.infolist_lv);
        searchIv = findViewById(R.id.info_iv_search);
        adapter = new InfoListAdapter();

        //添加监听事件
        searchIv.setOnClickListener(this);
        flushIv.setOnClickListener(this);
        showLv.setOnItemLongClickListener(this);
         //       showLv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //刷新
            case R.id.info_iv_flush:
                // 清空输入框
                searchEt.setText("");
                // mDatas清空
                mDatas.clear();
                //mDatas添加allFoodList数据
                for (FoodBean foodBean : allFoodList) {
                    mDatas.add(foodBean);
                }
                adapter.notifyDataSetChanged();
                break;
            case R.id.info_iv_search:
                // 获取输入框的文本
                String msg = searchEt.getText().toString().trim();
                if (TextUtils.isEmpty(msg)) {
                    Toast.makeText(this, "输入内容为空", Toast.LENGTH_SHORT).show();
                }
                ArrayList<FoodBean> list = new ArrayList<>();

                for (FoodBean foodBean : allFoodList) {
                    if (foodBean.getFoodName().contains(msg)) {
                        list.add(foodBean);
                    }
                }
                adapter = new InfoListAdapter(this, list);
                showLv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        long delete = mySQLiteOpenHelper.delete(allFoodList.get(i).getFoodName());
        Toast.makeText(this, delete == 0 ? "失败" : "成功", Toast.LENGTH_SHORT).show();
        allFoodList.clear();
        allFoodList = mySQLiteOpenHelper.findAll();
        mDatas.clear();
        mDatas.addAll(allFoodList);
        adapter = new InfoListAdapter(this, mDatas);
        showLv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return false;
    }
    public void setListener(){
        showLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                FoodBean foodBean = mDatas.get(i);
                Intent intent = new Intent(InfoListActivity.this, FoodDescActivity.class);
                intent.putExtra("foodBean", foodBean);
                startActivity(intent);
            }
        });

    }

}

(2)编辑activity_info_list.xml文件

2.1编辑好res--->laout--->activity_info_list.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:padding="10dp"
        android:background="@mipmap/search_background"
        >

        <ImageView
            android:id="@+id/info_iv_flush"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/clear_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            ></ImageView>


        <ImageView
            android:id="@+id/info_iv_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:layout_toLeftOf="@id/info_iv_flush"
            android:src="@mipmap/search_btn"></ImageView>

        <EditText
            android:id="@+id/info_et_searched"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@id/info_iv_search"
            android:background="@drawable/info_et_bg"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"></EditText>
    </RelativeLayout>
    <ListView
        android:id="@+id/infolist_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="1dp"
        ></ListView>

</LinearLayout>

(3)创建info_et_bg.xml文件

3.1在res--->drawable包下创建info_et_bg.xml文件

3.2修改info_et_bg.xml文件(绘制圆角矩形——作为文本输入框的背景)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dp"></corners>
    <stroke android:width="2dp" android:color="@color/blue"></stroke>
    <solid android:color="@color/yellow"></solid>
</shape>

(4)在color.xml文件中添加gray颜色

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="gray">#c5c5c5</color>
    <color name="blue">#b0e0e6</color>
    <color name="yellow">#EBE29C</color>
</resources>

2.2 任务二:使用SQLite初始化数据并编辑item_infolist_lv.xml布局

2.2.1 任务步骤

(1)在res包下的layout包中创建item_infolist_lv.xml文件

1.1在layout处鼠标右键创建Layout Resource File

1.2文件名为:item_infolist_lv,布局方式为相对布局

1.3编辑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"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/item_info_iv"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:src="@mipmap/beanmilk"
        android:scaleType="fitXY"
        ></ImageView>

    <TextView

        android:id="@+id/item_info_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/item_info_iv"
        android:text="牛奶"
        android:textStyle="bold"
        android:textSize="20sp"
        android:layout_marginLeft="10dp"
        ></TextView>

    <TextView
        android:id="@+id/item_info_tv_notmatch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/item_info_title"
       android:layout_alignBottom="@id/item_info_iv"
        android:text="酸醋柠檬橙子"
        ></TextView>

    <ImageView
        android:id="@+id/item_info_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/expand"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        ></ImageView>

</RelativeLayout>

2.2.2任务步骤

(1)在cn.edu.guit.health_231包下创建sql文件夹

(2)在sql包中导入FoodData类代码,如下图:

package cn.edu.guit.health_231.sql;


import cn.edu.guit.health_231.R;

public class FoodData {
    public static final String[] foodName = { "猪肉", "猪肝", "猪血", "羊肉", "牛肉", "牛肝", "鹅肉", "兔肉", "狗肉",
            "鸭肉", "鸡肉", "驴肉", "鸡蛋", "鲤鱼", "黄鱼", "虾", "虾皮", "螃蟹", "蛤", "鳖肉",
            "田螺", "大蒜", "葱", "萝卜", "芹菜", "韭菜", "菠菜", "莴笋", "竹笋", "西红柿", "洋葱",
            "醋", "茶", "豆浆", "红糖", "蜂蜜", "牛奶", "白酒", "啤酒" };
    public static final String[] notMatch = { "黄莲", "荞麦 雀肉 豆芽", "何首乌 地黄 黄豆 海带", "醋 红豆 半夏 南瓜",
            "橄榄 板粟 韭菜 ", "鲇鱼 鳗鱼 柿子", "狗肉 鲤鱼 柑橘", "鲤鱼 绿豆", "鳖", "鲤鱼", "金针菇",
            "豆浆 兔肉", "甘草 麦冬", "荞麦面 ", "富含维生素C的食物", "红枣 黄豆",
            "梨 柿子 茄子 花生仁 石榴 香瓜 芹菜 蜂蜜 西红柿", "芹菜 ", "鸭肉", "香瓜 木耳 牛肉 蚕豆 玉米",
            "地黄 何首乌 白术", "枣", "橘子 木耳", "黄瓜 蚬、蛤、蟹", "牛肉", "豆腐 鳝鱼 黄瓜", "蜂蜜",
            "糖浆", "白酒", "蜂蜜", "胡萝卜", "酒", "蜂蜜", "竹笋", "皮蛋", "豆腐 韭菜",
            "钙片果汁  药物 韭菜 柠檬", "胡萝卜 核桃 啤酒 红薯", "海鲜" };
    public static final int[] resId = { R.mipmap.pork, R.mipmap.pigliver, R.mipmap.pigblood,
            R.mipmap.lamb, R.mipmap.beef, R.mipmap.beefliver,
            R.mipmap.goose, R.mipmap.rabbit, R.mipmap.dog,
            R.mipmap.duck, R.mipmap.chicken, R.mipmap.donkey,
            R.mipmap.egg, R.mipmap.carp, R.mipmap.yellowfish,
            R.mipmap.shrimp, R.mipmap.shrimp2, R.mipmap.crab,
            R.mipmap.clam, R.mipmap.turtle, R.mipmap.riversnail,
            R.mipmap.garlic, R.mipmap.onion, R.mipmap.radish,
            R.mipmap.celery, R.mipmap.leek, R.mipmap.spinach,
            R.mipmap.lettuce, R.mipmap.bamboo, R.mipmap.tomato,
            R.mipmap.foreignonion, R.mipmap.vinegar, R.mipmap.tea,
            R.mipmap.beanmilk, R.mipmap.brownsuger, R.mipmap.honey,
            R.mipmap.milk, R.mipmap.whitespirit, R.mipmap.beer };
    public static final String [] foodDesc={"猪肉是目前人们餐桌上最常见的肉类食物之一,猪瘦肉中富含铁,容易被人体吸收,能预防贫血。猪肉味甘咸,性平,有补虚、滋阴、养血、润燥的功效。"
            ,"猪肝味甘、苦,性温,归肝经。猪肝富含多种营养素,是预防缺血性贫血的首选食品,猪肝中的铁容易被人体吸收,是理想的补肝、明目、养血的食品。"
            ,"猪血味甘、苦,性温,有解毒清肠、补血美容的功效。猪血富含维生素B2、维生素C、蛋白质、铁、磷、钙、尼克酸等营养成分。",
            "羊肉味甘、性温,具有补虚劳,祛寒冷,温补气血;益肾气,补形衰,开胃健力的功效。用于气血不足、腹部冷痛、体虚怕冷、腰膝酸痛、面黄肌瘦、血气两亏等一切虚状均有补益效果,最适宜冬季食用。",
            "牛肉有补中益气,滋养脾胃,强健筋骨,化痰息风,止渴止涎之功效。牛肉富含蛋白质,而脂肪少,氨基酸组成比猪肉更接近人体需要,能提高机体抗病能力。",
            "养血,补肝,明目。牛肝中铁质丰富,是补血食品中最常用的食物。"
            ,"鹅肉含有人体生长发育所必需的各种氨基酸,其组成接近人体所需氨基酸的比例,从生物学价值上来看,鹅肉是是理想的高蛋白、低脂肪、低胆固醇的营养健康食品,其亚麻酸含量超过其它肉类。鹅肉性平、味甘,具有益气补虚,和胃止渴的功效。",
            "兔肉质地细嫩,味道鲜美,营养丰富,与猪牛、羊肉相比较,兔肉营养成分独特,具有高铁、高钙、高磷脂和低脂肪、低胆固醇等特点,并且具有很高的消化率(可达85%),食后极易被消化吸收。",
            "狗肉温补脾胃、补肾助阳、壮力气、补血脉。狗肉不仅蛋白质含量高,而且蛋白质质量极佳,尤以球蛋白比例大,对增强机体抗病力和细胞活力及器官功能有明显作用。",
            "富含蛋白质、脂肪、铁、钾、糖等多种营养成分。其饱和脂肪酸的含量比猪肉、牛肉、羊肉少得多,脂肪酸熔点低,易于消化。所含胆固醇比鱼还要低一些。《本草纲目》记载:鸭肉主大补虚劳,最消毒热,利小便,除水肿,消胀满,利脏腑,退疮肿,定惊痫。",
            "鸡肉含丰富蛋白质,种类多,其含量比猪、牛、羊肉都高,而脂肪含量比其它肉类低,且多为不饱和脂肪酸,营养价值高,消化率高,很容易被人体吸收利用,有增强体力、强壮身体的作用。",
            "驴肉富含蛋白质,而且含有动物胶、骨胶原、钙、硫等成分,其中含有的高级不饱和脂肪酸,尤其是亚油酸、亚麻酸,对动脉硬化、冠心病、高血压有着良好的保健作用。中医认为:驴肉性味甘凉,有补气养血、滋阴壮阳、安神去烦功效。",
            "鸡蛋被认为是营养丰富的食品,含有蛋白质、脂肪、卵黄素、卵磷脂、维生素和铁、钙、钾等人体所需要的矿物质。鸡蛋味甘、性平,具有养心安神、补血、滋阴润燥的功效。",
            "鲤鱼的蛋白质不但含量高,而且质量也佳,人体消化吸收率可达96%,并能供给人体必需的氨基酸、矿物质、维生素A和维生素D。鲤鱼味甘、性平,具有健脾开胃、消水肿、利小便的作用。",
            "黄鱼含有丰富的蛋白质、微量元素和维生素,对人体有很好的补益作用。中医认为,黄鱼有和胃止血、益肾补虚、健脾开胃、安神止痢、益气填精之功效,对贫血、失眠、头晕、食欲不振及妇女产后体虚有良好疗效。",
            "虾营养极为丰富,含蛋白质是鱼、蛋、奶的几倍到几十倍;还含有丰富的钾、碘、镁、磷等矿物质及维生素A、氨茶碱等成分。",
            "虾皮中含有丰富的蛋白质和矿物质,尤其是钙的含量极为丰富,是缺钙者补钙的较佳途径之一。",
            "螃蟹含有丰富的蛋白质及钙、磷、铁等微量元素,对身体有很好的滋补作用。中医学认为螃蟹性寒、味咸,有清热、散血结、续断伤、理经脉和滋阴等功用;其壳可清热解毒、破淤清积止痛。",
            "肉味鲜美、营养丰富,蛋白质含量高,氨基酸的种类组成及配比合理; 脂肪含量低,不饱和脂肪酸较高,易被人体消化吸收。蛤味咸、性寒,有滋阴、利水、化痰、软坚的功效。",
            "鳖肉偏于滋阴补肾、补虚。是久病体弱,消瘦无力等人群的食疗佳品。鳖味咸、性平,具有滋阴凉血、平肝益气、散结软坚、消淤等功效。",
            "含蛋白质、脂肪、碳水化合物、钙、磷、铁、硫胺素、核黄素、尼克酸、维生素。田螺味咸、性寒,有清热利水、除湿解读的功效。",
            "大蒜含有丰富的营养成分,尤其是具有独特生物活性的蒜素等含硫化合物及硒蛋白等功能成分,这些物质具有显著的医用和食用价值。中医理论认为,大蒜生者辛热、熟者甘温,可除寒湿、怯阴邪、下气暖中、消谷化积、破恶血、攻冷积。",
            "葱的主要营养成分是蛋白质、糖类、维生素A原(主要在绿色葱叶中含有)、食物纤维以及磷、铁、镁等矿物质等。中医记载:葱能发汗解表,散寒通阳,解毒散凝。主治风寒感冒轻症,痈肿疮毒,痢疾脉微,寒凝腹痛,小便不利等病症。",
            "萝卜含有丰富的碳水化合物、纤维素、维生素C及钙、磷、钾、铁等矿物质,营养丰富;萝卜还有促进新陈代谢,增进食欲的作用,对于治疗消化不良、胃脘胀满、咳嗽多痰、胸闷气喘、伤风感冒有一定的疗效。",
            "芹菜含有蛋白质、脂肪、碳水化合物、纤维素、维生素、矿物质等营养成分。其中,维生素B、P的含量较多。矿物质元素钙、磷、铁的含量更是高于一般绿色蔬菜。",
            "韭菜性温,味辛,具有补肾起阳作用,且含有大量维生素和粗纤维,能增进胃肠蠕动,治疗便秘。",
            "菠菜含水量高(90%~93%),而热量低,是镁、铁、钾和维生素A的优质来源;也是钙和维生素C的上等来源。所含的磷、锌、叶酸和维生素B6也较为丰富。菠菜中含有的维生素C能够提高铁的吸收率,并促进铁与造血的叶酸共同作用,有效地预防贫血症。",
            "莴苣含蛋白质、脂粉、碳水化合物、钙、磷、铁、胡萝卜素、及维生素B、维生素C、尤其含烟酸较多。莴苣味道清新且略带苦味,可刺激消化酶分泌,增进食欲。其乳状浆液,可增强胃液、消化腺的分泌和胆汁的分泌,从而促进消化。",
            "具有滋阴凉血、和中润肠、清热化痰、解渴除烦、清热益气、利隔爽胃、利尿通便、解毒透疹、养肝明目、消食的功效,还可开胃健脾,宽肠利膈,通肠排便,开膈豁痰,消油腻,解酒毒。",
            "番茄富含维生素C,其含有的番茄红素有利尿和抑制细菌生长的作用,是优良的抗氧化剂,多吃番茄具有抗衰老作用,使皮肤保持白皙。西红柿味酸微甘、性平,具有生津止渴,健胃消食,凉血平肝,清热解毒,降低血压之功效。",
            "洋葱所含的微量元素硒是一种很强的抗氧化剂,能清除体内的自由基,增强细胞的活力和代谢能力,具有抗衰老的功效。",
            "醋可以开胃,促进唾液和胃液的分泌,帮助消化吸收,使食欲旺盛,消食化积。醋还有很好的抑菌和杀菌作用,能有效预防肠道疾病、流行性感冒和呼吸疾病。",
            "茶叶内含化合物500种左右,有些是人体必需的营养成分。如维生素类、蛋白质、氨基酸、类脂类、糖类及矿物质元素等,对人体有较高营养价值。有些是对人体有保健和药用价值的成分,如茶多酚、咖啡碱、脂多糖等,常喝茶有利于人体健康。",
            "豆浆含有丰富的植物蛋白,磷脂,维生素B1、B2,烟酸和铁、钙等矿物质,尤其是钙的含量,虽不及豆腐高,但比其他任何乳类都丰富。豆浆是防治高血脂、高血压、动脉硬化等疾病的理想食品。",
            "红糖中所含有的葡萄糖、果糖等多种单糖和多糖类能量物质,为人体提供能量。",
            "蜂蜜含有70%以上的转化糖,能够被人体肠壁细胞直接吸收利用,没有必要经人体消化,经常服用蜂蜜,能帮助消化。",
            "牛奶的营养成分很高,牛奶中的矿物质种类也非常丰富,除了我们所熟知的钙以外,磷、铁、锌、铜、锰、钼的含量都很多。牛奶是人体钙的最佳来源,而且钙磷比例非常适当,利于钙的吸收。牛奶中含组成人体蛋白质的氨基酸有20种,其中有8种是人体本身不能合成的,为必需氨基酸,牛奶是人类最理想的天然食品。",
            "白酒主要由水乙醇和少量微量元素组成。夜晚服用少量的白酒,可平缓的促进血液循环,起到催眠作用。饮少量白酒可刺激胃液分泌与唾液分泌,因而起到健胃和止疼痛、利小便及驱虫的作用。白酒味苦、甘、辛,性温,有毒,入心、肝、肺、胃经。",
            "啤酒营养丰富,其中含有丰富的氨基酸,是原料大麦含有的蛋白质经过酶的作用分解而产生的。且啤酒中还含有多种维生素。适量饮啤酒有强心、利尿、健胃的功效。"};
}

(3)在sql中创建MySQLiteOpenHelper类(继承 SQLiteOpenHelper,完善相关代码——初始化数据源)

3.1在sql中创建MySQLiteOpenHelper类

3.2完善MySQLiteOpenHelper类代码

package cn.edu.guit.health_231.sql;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import cn.edu.guit.health_231.bean.FoodBean;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "foodDB";
    private static final String TABLE_NAME = "foodTABLE";

    private static final String foodName = "foodName";
    private static final String notMatch = "notMatch";
    private static final String foodDesc = "foodDesc";
    private static final String picId = "picId";
    private static final int DB_VERSION = 1;

    private static final String CRATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
            + " ( foodName TEXT, notMatch TEXT,  picId INTEGER,foodDesc TEXT ) ";

    public MySQLiteOpenHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CRATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public long insertData(FoodBean foodBean) {
        ContentValues values = new ContentValues();
        SQLiteDatabase database = getWritableDatabase();
        values.put(foodName, foodBean.getFoodName());
        values.put(notMatch, foodBean.getNotMatch());
        values.put(picId, foodBean.getResId());
        values.put(foodDesc, foodBean.getFoodDesc());
        long row = database.insert(TABLE_NAME, null, values);
        return row;
    }

    public void initDate() {
        for (int i = 0; i < FoodData.foodName.length; i++) {
            String foodName = FoodData.foodName[i];
            String notMatch = FoodData.notMatch[i];
            int picId = FoodData.resId[i];
            String foodDesc = FoodData.foodDesc[i];
            FoodBean foodBean = new FoodBean(foodName, notMatch, picId, foodDesc);
            insertData(foodBean);
        }
    }

    public List<FoodBean> findAll() {
        List<FoodBean> list = new ArrayList<>();
        SQLiteDatabase database = getWritableDatabase();
        Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex(foodName));
                @SuppressLint("Range") String not_match = cursor.getString(cursor.getColumnIndex(notMatch));
                @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex(picId));
                @SuppressLint("Range") String desc = cursor.getString(cursor.getColumnIndex(foodDesc));
                FoodBean foodBean = new FoodBean(name, not_match, id, desc);
                list.add(foodBean);
            }
            cursor.close();
        }
        return list;
    }

    public long delete(String name) {
        SQLiteDatabase database = getWritableDatabase();
        String delWhere = "foodName = '" + name + "'";
        int row = database.delete(TABLE_NAME, delWhere, null);
        return row;
    }
}

3.3在MySQLiteOpenHelper类中添加insertData方法(添加数据的方法)

3.4在cn.edu.guit.health_231包下创建bean文件夹,在bean中创建FoodBean实体类(包含String foodName、String notMatch、int resId、String foodDesc)

 

package cn.edu.guit.health_231.bean;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;

public class FoodBean implements Parcelable {
    String foodName;
    String notMatch;
    int resId;
    String foodDesc;

    public FoodBean() {
    }

    public FoodBean(String foodName, String notMatch, int resId, String foodDesc) {
        this.foodName = foodName;
        this.notMatch = notMatch;
        this.resId = resId;
        this.foodDesc = foodDesc;
    }

    protected FoodBean(Parcel in) {
        foodName = in.readString();
        notMatch = in.readString();
        resId = in.readInt();
        foodDesc = in.readString();
    }

    public static final Creator<FoodBean> CREATOR = new Creator<FoodBean>() {
        @Override
        public FoodBean createFromParcel(Parcel in) {
            return new FoodBean(in);
        }

        @Override
        public FoodBean[] newArray(int size) {
            return new FoodBean[size];
        }
    };

    public String getFoodName() {

        return foodName;
    }

    public void setFoodName(String foodName) {

        this.foodName = foodName;
    }

    public String getNotMatch() {

        return notMatch;
    }

    public void setNotMatch(String notMatch) {

        this.notMatch = notMatch;
    }

    public int getResId() {
        return resId;
    }

    public void setResId(int resId) {

        this.resId = resId;
    }

    public String getFoodDesc() {

        return foodDesc;
    }

    public void setFoodDesc(String foodDesc) {

        this.foodDesc = foodDesc;
    }

    @Override
    public String toString() {
        return "FoodBean{" +
                "foodName='" + foodName + '\'' +
                ", notMatch='" + notMatch + '\'' +
                ", resId=" + resId +
                ", foodDesc='" + foodDesc + '\'' +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(foodName);
        parcel.writeString(notMatch);
        parcel.writeInt(resId);
        parcel.writeString(foodDesc);
    }
}

3.5在MySQLiteOpenHelper类中添加initData方法(初始化表格数据的方法)


    public void initDate() {
        for (int i = 0; i < FoodData.foodName.length; i++) {
            String foodName = FoodData.foodName[i];
            String notMatch = FoodData.notMatch[i];
            int picId = FoodData.resId[i];
            String foodDesc = FoodData.foodDesc[i];
            FoodBean foodBean = new FoodBean(foodName, notMatch, picId, foodDesc);
            insertData(foodBean);
        }
    }

3.6在MySQLiteOpenHelper类中添加findAll方法(查询表格所有数据的方法)


    public List<FoodBean> findAll() {
        List<FoodBean> list = new ArrayList<>();
        SQLiteDatabase database = getWritableDatabase();
        Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex(foodName));
                @SuppressLint("Range") String not_match = cursor.getString(cursor.getColumnIndex(notMatch));
                @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex(picId));
                @SuppressLint("Range") String desc = cursor.getString(cursor.getColumnIndex(foodDesc));
                FoodBean foodBean = new FoodBean(name, not_match, id, desc);
                list.add(foodBean);
            }
            cursor.close();
        }
        return list;
    }

(4)在cn.edu.guit.health_231包下创建food_list包,将 InfoListActivity类拖动到food_list包中,并在food_list包中创建 InfoListAdapter类(继承BaseAdapter,完善相关代码)

4.1在cn.edu.guit.health_学号后三位包下创建food_list包

4.2将InfoListActivity类拖动到 food_list包中

4.3在food_list包中创建InfoListAdapter类(继承BaseAdapter,完善相关代码)

package cn.edu.guit.health_231.food_list;

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;

import cn.edu.guit.health_231.R;
import cn.edu.guit.health_231.bean.FoodBean;


public class InfoListAdapter extends BaseAdapter {
    private Context context;
    private List<FoodBean> mdatas;


    public InfoListAdapter() {
    }

    public InfoListAdapter(Context context, List<FoodBean> mdatas) {
        this.context = context;
        this.mdatas = mdatas;
    }


    @Override
    public int getCount() {
        return mdatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mdatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_infolist_lv, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        FoodBean foodBean = mdatas.get(i);
        viewHolder.titleIv.setText(foodBean.getFoodName());
        viewHolder.notIv.setText("不可匹配食物" + foodBean.getNotMatch());
        viewHolder.iv.setImageResource(foodBean.getResId());
        return view;
    }

    class ViewHolder {
        ImageView iv;
        TextView titleIv, notIv;

        public ViewHolder(View view) {
            iv = view.findViewById(R.id.item_info_iv);
            titleIv = view.findViewById(R.id.item_info_title);
            notIv = view.findViewById(R.id.item_info_tv_notmatch);
        }
    }
}

2.3 任务三:创建InfoListAdapter类并完善InfoListActivity类代码实现列表展示

2.3.1 任务步骤

(1)创建food_list文件夹,将InfoListActivity类拖入其中

(2)在food_list文件夹中创建InfoListAdapter类(继承BaseAdapter 重写方法,并完善相关代码)

2.3.2任务步骤

(1)完善InfoListActivity类代码

package cn.edu.guit.health_231.food_list;

import android.annotation.SuppressLint;
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 androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import cn.edu.guit.health_231.R;
import cn.edu.guit.health_231.bean.FoodBean;
import cn.edu.guit.health_231.food_desc.FoodDescActivity;
import cn.edu.guit.health_231.sql.MySQLiteOpenHelper;


public class InfoListActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener {
    EditText searchEt;
    ImageView searchIv, flushIv;
    ListView showLv;
    List<FoodBean> mDatas = new ArrayList<>();   //视图要显示的数据集合
    List<FoodBean> allFoodList = new ArrayList<>();//从数据库查询出所有的数据
    InfoListAdapter adapter = null;
    MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_list);
        initView();
        mySQLiteOpenHelper.initDate();
        allFoodList = mySQLiteOpenHelper.findAll();
        mDatas.addAll(allFoodList);
        adapter = new InfoListAdapter(this, mDatas);
        showLv.setAdapter(adapter);
        setListener();
    }

    @SuppressLint("WrongViewCast")
    protected void initView() {
        //控件的声明info_et_searched
        searchEt = findViewById(R.id.info_et_searched);
        flushIv = findViewById(R.id.info_iv_flush);
        showLv = findViewById(R.id.infolist_lv);
        searchIv = findViewById(R.id.info_iv_search);
        adapter = new InfoListAdapter();

        //添加监听事件
        searchIv.setOnClickListener(this);
        flushIv.setOnClickListener(this);
        showLv.setOnItemLongClickListener(this);
         //       showLv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //刷新
            case R.id.info_iv_flush:
                // 清空输入框
                searchEt.setText("");
                // mDatas清空
                mDatas.clear();
                //mDatas添加allFoodList数据
                for (FoodBean foodBean : allFoodList) {
                    mDatas.add(foodBean);
                }
                adapter.notifyDataSetChanged();
                break;
            case R.id.info_iv_search:
                // 获取输入框的文本
                String msg = searchEt.getText().toString().trim();
                if (TextUtils.isEmpty(msg)) {
                    Toast.makeText(this, "输入内容为空", Toast.LENGTH_SHORT).show();
                }
                ArrayList<FoodBean> list = new ArrayList<>();

                for (FoodBean foodBean : allFoodList) {
                    if (foodBean.getFoodName().contains(msg)) {
                        list.add(foodBean);
                    }
                }
                adapter = new InfoListAdapter(this, list);
                showLv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        long delete = mySQLiteOpenHelper.delete(allFoodList.get(i).getFoodName());
        Toast.makeText(this, delete == 0 ? "失败" : "成功", Toast.LENGTH_SHORT).show();
        allFoodList.clear();
        allFoodList = mySQLiteOpenHelper.findAll();
        mDatas.clear();
        mDatas.addAll(allFoodList);
        adapter = new InfoListAdapter(this, mDatas);
        showLv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return false;
    }
    public void setListener(){
        showLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                FoodBean foodBean = mDatas.get(i);
                Intent intent = new Intent(InfoListActivity.this, FoodDescActivity.class);
                intent.putExtra("foodBean", foodBean);
                startActivity(intent);
            }
        });

    }

}

(2)实现搜索和刷新图标相应功能

2.1 InfoListActivity类实现OnClickListener接口

2.2在InfoListActivity类的initView方法中为两个图标添加事件监听

2.3重写OnClickListener的onClick方法

 

2.4 任务四:创建FoodGridActivity活动并编辑activity_food_grid.xml和item_foodgrid.xml文件

2.4.1 任务步骤

(1)InfoListActivity类实现OnClickListener接口

(2)在InfoListActivity类的initView方法中为两个图标添加事件监听

(3)重写OnClickListener的onClick方法

(4)在cn.edu.guit.health_231包下创建food_grid包

   4.1在cn.edu.guit.health_学号后三位包下鼠标右键new--->package---->food_grid

(5)food_grid包中创建FoodGridActivity活动

5.1在food_grid包右键new--->Activity--->Empty Activity---->FoodGridActivity

2.4.2任务步骤

(1)编辑activity_food_grid.xml文件

1.1编辑好layout包下的activity_food_grid.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
  <RelativeLayout
      android:id="@+id/food_grid_layout"
      android:layout_width="match_parent"
      android:layout_height="60dp"
      android:background="@mipmap/search_background"
      >

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:text="健康饮食"
          android:textColor="@color/white"
          android:textSize="20sp"
          android:textStyle="bold"></TextView>
  </RelativeLayout>
    <GridView
        android:id="@+id/food_grid_gv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        ></GridView>


</LinearLayout>

(2)在values包下的colors.xml文件中添加自定义颜色

(3)在drawble包下创建item_gridbg.xml文件(作为子布局页面的背景图)

3.1文件名为item_gridbg,修改为shape

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dp"></corners>
    <stroke android:width="2dp" android:color="@color/blue"></stroke>
    <solid android:color="@color/yellow"></solid>
</shape>

(4)创建item_foodgrid.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"
    android:layout_margin="10dp"
    android:background="@drawable/item_gridbg"
    >

    <ImageView
        android:id="@+id/item_grid_iv"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:scaleType="fitXY"
        android:src="@mipmap/beef"></ImageView>
    <TextView
        android:id="@+id/item_grid_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="牛肉"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_below="@id/item_grid_iv"
        android:layout_centerInParent="true"
        ></TextView>
</RelativeLayout>

2.5 任务五:创建FoodGridAdapter类实现FoodGridActivity活动页面数据显示并创建FoodDescActivity活动

2.5.1 任务步骤

(1)在food_grid包中创建FoodGridAdapter类(继承BaseAdapter)

1.1在food_grid包中鼠标右键new--->Java Class,创建名为FoodGridAdapter的类,继承 BaseAdapter

1.2完善FoodGridAdapter类代码

package cn.edu.guit.health_231.food_grid;

import android.annotation.SuppressLint;
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;

import cn.edu.guit.health_231.R;
import cn.edu.guit.health_231.bean.FoodBean;


public class FoodGridAdapter extends BaseAdapter {
    private Context context;
    private List<FoodBean> mData;


    public FoodGridAdapter() {
    }

    public FoodGridAdapter(Context context, List<FoodBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ResourceType")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view==null){
            view = LayoutInflater.from(context).inflate(R.layout.item_foodgrid,null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        FoodBean foodBean = mData.get(i);
        holder.iv.setImageResource(foodBean.getResId());
        holder.titleIv.setText(foodBean.getFoodName());
        return view;

    }
    class ViewHolder{
        ImageView iv;
        TextView titleIv;

        public ViewHolder(View view) {
            this.iv = view.findViewById(R.id.item_grid_iv);
            this.titleIv =  view.findViewById(R.id.item_grid_tv);

        }
    }
}

(2)完善FoodGridActivity类代码实现数据显示

package cn.edu.guit.health_231.food_grid;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import cn.edu.guit.health_231.R;
import cn.edu.guit.health_231.bean.FoodBean;
import cn.edu.guit.health_231.food_desc.FoodDescActivity;
import cn.edu.guit.health_231.sql.MySQLiteOpenHelper;


public class FoodGridActivity extends AppCompatActivity {
    List<FoodBean> mDatas = new ArrayList<>();   //视图要显示的数据集合
    List<FoodBean> allFoodList = new ArrayList<>();//从数据库查询出所有的数据
    FoodGridAdapter adapter = null;
    GridView gridView;
    MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_grid);
        initView();
        mySQLiteOpenHelper.initDate();
        allFoodList = mySQLiteOpenHelper.findAll();
        mDatas.addAll(allFoodList);
        adapter = new FoodGridAdapter(this, mDatas);
        gridView.setAdapter(adapter);
        setListener();
    }

    @SuppressLint("WrongViewCast")
    protected void initView() {
        //控件的声明
        gridView = findViewById(R.id.food_grid_gv);

    }
    public void setListener(){
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                FoodBean foodBean = mDatas.get(i);
                Intent intent = new Intent(FoodGridActivity.this, FoodDescActivity.class);
                intent.putExtra("foodBean", foodBean);
                startActivity(intent);
            }
        });
    }
}

2.5.2任务步骤

(1)在cn.edu.guit.health_学号后三位包下创建food_desc包

1.1创建名为food_desc的文件夹

(2)在food_desc包中创建FoodDescActivity活动

(3)编辑相对应的activity_food_desc.xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@mipmap/search_background">

            <ImageView
                android:id="@+id/fooddesc_iv_back"
                android:layout_width="80dp"
                android:layout_height="40dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:src="@mipmap/btn_back"></ImageView>

            <TextView
                android:id="@+id/fooddesc_tv_title1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="牛肉"
                android:textSize="22sp"
                android:textStyle="bold"
                android:textColor="@color/white"
                ></TextView>
        </RelativeLayout>

        <ImageView
            android:id="@+id/fooddesc_iv_bigpic"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:layout_centerVertical="true"
            android:layout_margin="20sp"
            android:src="@mipmap/beef"
            android:scaleType="fitXY"
            ></ImageView>

        <TextView
            android:id="@+id/fooddesc_tv_title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="牛肉"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/purple_700"
            android:layout_gravity="center"
            ></TextView>

        <TextView
            android:id="@+id/fooddesc_tv_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="这是牛肉牛肉"
            android:textSize="16sp"
            android:layout_marginLeft="20dp"
            ></TextView>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@mipmap/food_info_middle_background"
            android:text="不宜同食的食物列表"
            android:textSize="16sp"
            android:textStyle="bold"
            android:padding="10dp"
            android:textColor="@color/white"
            ></TextView>

        <TextView
            android:id="@+id/fooddesc_tv_notmatch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="橄榄菜,韭菜"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginLeft="20dp"
            ></TextView>
    </LinearLayout>


</ScrollView>

(4)完善HomeMenuActivity类代码实现按钮点击跳转


    public void onClick(View view){
        Intent intent = new Intent();
        switch (view.getId()){
            case R.id.home_bnt1:
                intent.setClass(HomeMenuActivity.this, InfoListActivity.class);
                break;
            case R.id.home_bnt2:
                intent.setClass(HomeMenuActivity.this, FoodGridActivity.class);
                break;
            case R.id.home_bnt3:
                intent.setClass(HomeMenuActivity.this, AboutMeActivity.class);
                break;
            case R.id.home_bnt4:
                intent.setClass(HomeMenuActivity.this, SlitherActivity.class);
                break;
        }

(5)完善FoodListActivity类代码实现点击食物跳转到 FoodDescActivity

5.1在FoodListActivity类的onCreate方法中设置listView的单向点击监听

2.6 任务六:编辑FoodDescActivity类代码实现好组件item点击跳转功能并创建AboutMeActivity活动

2.6.1 任务步骤

(1)完善HomeMenuActivity类代码实现按钮点击跳转

(2)完善FoodGridActivity类代码实现点击食物跳转到 FoodDescActivity

2.1在FoodGridActivity类的onCreate方法中设置gridView的单向点击监听

(3)实现FoodDescActivity食物详情数据显示

2.6.2任务步骤

(1)创建about_me包,在about_me包中创建AboutMeActivity活动

1.1在cn.edu.guit.health_学号后三位包中鼠标右键new---->package----->创建名为 about_me的包

1.2在about_me包中鼠标右键new---->activity---->empty activity---->创建名为 AboutMeActivity活动

(2)编辑好对应的activity_about_me.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/about_vp"
        android:layout_width="match_parent"
        android:layout_height="230dp"></androidx.viewpager.widget.ViewPager>

    <TextView
        android:id="@+id/about_tv_version"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:drawableLeft="@mipmap/icon_door"
        android:drawablePadding="20dp"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="当前版本v1.0"
        android:textSize="18sp"
        android:textStyle="bold"></TextView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"></TextView>

    <TextView
        android:id="@+id/about_tv_word"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:drawableLeft="@mipmap/icon_world"
        android:drawablePadding="20dp"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="联系电话:18269161314"
        android:textSize="18sp"
        android:textStyle="bold"></TextView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"></TextView>

    <TextView
        android:id="@+id/about_tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:drawableLeft="@mipmap/icon_phone"
        android:drawablePadding="20dp"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="学校地址:桂林信息科技学院"
        android:textSize="18sp"
        android:textStyle="bold"></TextView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"></TextView>

    <TextView
        android:id="@+id/about_tv_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:drawableLeft="@mipmap/icon_flash"
        android:drawablePadding="20dp"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="分享软件"
        android:textSize="18sp"
        android:textStyle="bold"></TextView>
</LinearLayout>

(3)在layout包中创建名为item_aboutvp.xml文件(作为轮播图的模板)

3.1在layout包中鼠标右键new--->Layout Resource Files--->创建名为item_aboutvp.xml 的文件

3.2修改item_aboutvp.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="230dp">
<ImageView
    android:id="@+id/item_aboutvp_iv"
    android:layout_width="match_parent"
    android:layout_height="230dp"
    android:src="@mipmap/ab1"
    android:scaleType="centerCrop"
    ></ImageView>
</RelativeLayout>

2.7 任务七:实现AboutMeActivity活动功能并修改layout_main.xml文件

2.7.1 任务步骤

(1)在layout包中创建名为item_aboutvp.xml文件(作为轮播图的模板)

1.1在layout包中鼠标右键new--->Layout Resource Files--->创建名为item_aboutvp.xml 的文件

1.2在item_aboutvp.xml文件中进行如下设置:

(2)创建AboutMeAdapter适配器

2.1在about_me包中创建名为AboutMeAdapter(继承PagerAdapter)

package cn.edu.guit.health_231.about_me;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class AboutMeAdapter extends PagerAdapter {
    List<View> viewList;

    public AboutMeAdapter(List<View> viewList) {
        this.viewList = viewList;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = viewList.get(position % viewList.size());
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        View view = viewList.get(position % viewList.size());
        container.removeView(view);
    }
}

(3)完善AboutMeActivity类代码实现相应功能

package cn.edu.guit.health_231.about_me;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.edu.guit.health_231.R;


public class AboutMeActivity extends AppCompatActivity {
    // 声明控件
    ViewPager aboutVP;
    TextView shareTv;
    List<View> viewList = new ArrayList<>();
    private int index = 0;
    Timer timer = new Timer();
    int[] picIds = {R.mipmap.ab1, R.mipmap.ab2, R.mipmap.ab3, R.mipmap.ab4, R.mipmap.ab5};
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            //aboutVP.setCurrentItem(picIds[index++ % picIds.length]);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_me);
        // 查找控件
        aboutVP = findViewById(R.id.about_vp);
        shareTv = findViewById(R.id.about_tv_share);
        for (int i = 0; i < picIds.length; i++) {
            ImageView imageView = new ImageView(AboutMeActivity.this);
            imageView.setImageResource(picIds[index % picIds.length]);
            viewList.add(imageView);
        }


        // 2
        aboutVP.addView(viewList.get(0));
        // 3
        AboutMeAdapter aboutMeAdapter = new AboutMeAdapter(viewList);
        // 4
        aboutVP.setAdapter(aboutMeAdapter);
        // 5
        aboutMeAdapter.notifyDataSetChanged();


        shareTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(AboutMeActivity.this, "分享成功!", Toast.LENGTH_SHORT).show();
            }
        });
        myRun();
    }
    void myRun(){
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what=index++ % picIds.length;
                handler.sendMessage(message);
            }
        },0,50);
    }
}

2.7.2任务步骤

(1)修改layout_main.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="match_parent"
    android:background="@mipmap/home"
    >
    <TextView
        android:id="@+id/main_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_alignParentRight="true"
        android:layout_margin="30dp"
        ></TextView>



</RelativeLayout>

2.8 任务八:实现MainActivity活动5秒跳转到HomeMenuActivity功能

2.8.1 任务步骤

(1)完善MainActivity类代码实现功能

1.1完善相关代码

package cn.edu.guit.health_231;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    int time = 5;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 100) {
                time--;
                if (time != 0) {
                    textView.setText(time + "");
                    handler.sendEmptyMessageDelayed(100, 1000);
                } else {
                    Intent intent = new Intent(MainActivity.this, HomeMenuActivity.class);
                    startActivity(intent);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.main_tv);
        handler.sendEmptyMessageDelayed(100, 1000);


    }
}

(2)运行调试(修改AndroidManifest.xml文件)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.guit.health_231">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Health_231">

        <activity
            android:name=".about_me.AboutMeActivity"
            android:exported="false" />
        <activity android:name=".food_list.InfoListActivity" />
        <activity
            android:name=".HomeMenuActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".food_grid.FoodGridActivity" />
        <activity android:name=".food_desc.FoodDescActivity" />
        <activity android:name=".Slither.SlitherActivity" />
    </application>

</manifest>

2.8.2任务步骤

(1)创建签名文件

2.9 拓展任务:完善SQLite数据库重复数据的删除功能与项目上架“应用宝”商店

2.9.1 任务步骤

2.9.2任务步骤

3  每个任务最终效果

任务一

任务二

任务三

任务四

任务五

任务六

任务七

7.1点击按钮实现相应的页面跳转

7.2InfoListActivity和FoodGridActivity活动数据能够显示

7.3AboutMeActivity活动页轮播图正常显示

7.4layout包下的layout_main.xml文件页面如下图一

任务八

4  调试所遇问题及解决方式

1.在跳转类的时候忘记添加setListener();  以至于点击的时候app闪退,后面检查了好久才找出来。

2.由于清单文件没有添加跳转,导致app点击没反应,后面重新添加才可以正常运行

5  实训心得体会

基于安卓开发的健康饮食实训结束了,通过对健康饮食搭配的开发设计,我对Android 应用开发有了更为深层次的理解,对手机应用也有了全新的认识于熟练,对基于Android多种布局以及应用上架有了更深层次的理解,美中不足的是应用没能成功上架。实训前对android的应用操作还不熟悉,实训结束后终于知道了步骤操作。还完成了一些额外的扩展,增强了app的可玩性,学到了不少知识。本健康饮食app基本上实现了需求中的全部功能需求,整体上较为满意。

本应用的难点在于如何设置的不同布局,以及跳转,实现进入不同界面的效果。学完后,加深了对布局的理解,为以后开发类似程序打下了基础。但是自己对安卓的很多方面还不是很了解,各种方法也不是很会使用,实训过后自己还会加强对安卓应用设计的学习。

6  参考文献

[1] 刘望舒.Android进阶之光(第2版).北京:电子工业出版社,2021

[2] 李刚.疯狂Android讲义(第4版).北京:电子工业出版社,2019

[3] 欧阳燊.Android Studio开发实战(第2版).北京:清华大学出版社,2018

项目源码:

链接:https://pan.baidu.com/s/1UmE7iQGJ7QhPvhjH0VGzHw 
提取码:1314

猜你喜欢

转载自blog.csdn.net/qq_57749830/article/details/126236987