package com.wangsanshi.weather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wangsanshi.weather.R;
import com.wangsanshi.weather.entity.ItemHourlyBean;

import java.util.List;

public class LvHourlyAdapter extends BaseAdapter {
    private List<ItemHourlyBean> data;
    private LayoutInflater inflater;

    public LvHourlyAdapter(Context context, List<ItemHourlyBean> data) {
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

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

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HourlyViewHolder holder;

        if (convertView == null) {
            holder = new HourlyViewHolder();
            convertView = inflater.inflate(R.layout.item_lv_hourly_weather, parent, false);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time_hourly);
            holder.tvTemp = (TextView) convertView.findViewById(R.id.tv_temp_hourly);
            holder.tvHumidity = (TextView) convertView.findViewById(R.id.tv_humidity_hourly);
            holder.tvPres = (TextView) convertView.findViewById(R.id.tv_pres_hourly);
            convertView.setTag(holder);
        } else {
            holder = (HourlyViewHolder) convertView.getTag();
        }

        holder.tvTime.setText("  " + data.get(position).getItemTime().split(" ")[1]);
        holder.tvTemp.setText("  " + data.get(position).getItemTemp() + "°");
        holder.tvHumidity.setText("  " + data.get(position).getItemHumidity() + "%");
        holder.tvPres.setText("  " + data.get(position).getItemPres() + "hpa");

        return convertView;
    }

    class HourlyViewHolder {
        TextView tvTime;
        TextView tvTemp;
        TextView tvHumidity;
        TextView tvPres;
    }
}
