You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
foodcheck/FoodGridAdapter.java

66 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.example.health.food_grid;
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.health.Bean.FoodBean;
import com.example.health.R;
import java.util.List;
public class FoodGridAdapter extends BaseAdapter {
Context context;
public FoodGridAdapter(Context context, List<FoodBean> mDates) {
this.context = context;
this.mDates = mDates;
}
List<FoodBean>mDates;
@Override
public int getCount() {
return mDates.size();
}
@Override
public Object getItem(int position) {
return mDates.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//声明ViewHolder
ViewHolder holder = null;
if (convertView == null) {//判断是否有复用的view如果没有就创建
convertView = LayoutInflater.from(context).inflate(R.layout.item_foodgrid,null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//获取指定位置的数据
FoodBean foodBean = mDates.get(position);
holder.iv.setImageResource(foodBean.getPicID());
holder.tv.setText(foodBean.getFoodName());
return convertView;
}
class ViewHolder{
ImageView iv;
TextView tv;
public ViewHolder(View view){
iv=view.findViewById(R.id.item_grid_iv);
tv=view.findViewById(R.id.item_grid_tv);
}
}
}