package com.example.administrator.zhapp.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.example.administrator.zhapp.DBModel.Zy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.entity.HgInfo;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/10/21.
 */

public class SpinnerAdapter<T> extends BaseAdapter {

    private List<T> list;
    private Context context;
    private String type = "";

    public void setType(String type) {

        this.type = type;
    }

    public SpinnerAdapter(List<T> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.spinner_item, null, false);
            holder.tv = (TextView) convertView.findViewById(R.id.item);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Object object = list.get(position);

        if (type.equals("")) {

            if (object instanceof Zy) {
                holder.tv.setText(((Zy) object).getVALUE());
            } else if (object instanceof HgInfo.Hg) {
                holder.tv.setText(((HgInfo.Hg) object).getBinnum());
            }
        } else {
            holder.tv.setText(((HgInfo.Hg) object).getLotnum());
        }

        return convertView;
    }
}

class ViewHolder {
    TextView tv;
}
