团队冲刺第二十一天

今天继续了饮食方面的完善,完善了大体的布局和显示界面,主要代码如下,但是一些食物的搭配类和相关的查询还未完工,明天继续此内容。

package com.example.myapplication.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 com.example.myapplication.R;
import com.example.myapplication.bean.FoodBean;

import java.util.List;

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

    public InfoListAdapter(Context context,List<FoodBean> mDatas){
        this.context=context;
        this.mDatas=mDatas;
    }
    //设定了ListView列表展示的行数
    @Override
    public int getCount() {
        return mDatas.size();
    }
    //返回指定的位置对应的数据
    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder=null;
       if(convertView==null){
           //将布局转换成view计算的方法
           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 null;
    }

    class ViewHolder{
        ImageView iv;
        TextView titleTv,notTv;
        public ViewHolder(View view){
            iv=view.findViewById(R.id.item_info_iv);
            titleTv=view.findViewById(R.id.item_info_tv_title);
            notTv=view.findViewById(R.id.item_info_tv_notmatch);
        }
    }
}
package com.example.myapplication.food_list;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.myapplication.R;
import com.example.myapplication.bean.FoodBean;
import com.example.myapplication.bean.FoodUtils;

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

public class InfoListActivity extends AppCompatActivity {
    EditText searchEt;
    ImageView searchIv,flushIv;
    ListView showLv;
    //ListView内容数据源
    List<FoodBean> mDatas;
    List<FoodBean> allFoodList;
    private InfoListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_list);
        
        //查找空间
        initView();
        //找到对应的数据源
        mDatas=new ArrayList<>();
        allFoodList= FoodUtils.getAllFoodList();
        mDatas.addAll(allFoodList);
        //创建适配器
        adapter=new InfoListAdapter(this,mDatas);
        //设置适配器
        showLv.setAdapter(adapter);

    }

    private void initView() {
        searchEt=findViewById(R.id.info_et_search);
        searchIv=findViewById(R.id.info_iv_search);
        flushIv=findViewById(R.id.info_iv_flush);
        showLv=findViewById(R.id.infolist_lv);

    }
}
<?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=".food_list.InfoListActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:padding="10dp"
        android:background="#00FFFF">
        <ImageView
            android:id="@+id/info_iv_flush"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/flush"
            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/search"
            android:layout_toLeftOf="@+id/info_iv_flush"
            android:layout_centerVertical="true"
            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:layout_toLeftOf="@+id/info_iv_search"
            android:paddingLeft="20dp"
            android:background="@drawable/info_et_bg"
            android:layout_centerVertical="true"
            android:textColor="@color/white"/>

    </RelativeLayout>

    <ListView
        android:id="@+id/infolist_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/dividerColor"
        android:dividerHeight="1dp">
    </ListView>

</LinearLayout>

 

posted @ 2020-05-25 22:52  遇码则码队  阅读(156)  评论(0编辑  收藏  举报