package com.ifavine.pay.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.ifavine.pay.R;
import com.ifavine.pay.vo.VersionBean;

import java.util.List;

/**
 * Created by Administrator on 2018/10/23.
 */

public class ReportMonthsAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<VersionBean> mDatas;

    //MyAdapter需要一个Context，通过Context获得Layout.inflater，然后通过inflater加载item的布局
    public ReportMonthsAdapter(Context context, List<VersionBean> datas) {

        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    //返回数据集的长度
    @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;
    }

    //这个方法才是重点，我们要为它编写一个ViewHolder
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_report_months, parent, false); //加载布局
            holder = new ViewHolder();


            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
        }

        VersionBean bean = mDatas.get(position);
        holder.tv_name.setText(bean.link);
        holder.tv_num.setText(bean.os_type);
        holder.tv_amount.setText(bean.version_description);
        holder.tv_company.setText(bean.version_id);
        return convertView;
    }

    //这个ViewHolder只能服务于当前这个特定的adapter，因为ViewHolder里会指定item的控件，不同的ListView，item可能不同，所以ViewHolder写成一个私有的类
    private class ViewHolder {
        TextView tv_name;
        TextView tv_num;
        TextView tv_amount;
        TextView tv_company;
    }
}
