package com.tofly.ghrq_route.common.ui.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.esri.arcgisruntime.data.Field;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.map.domain.ConfigAll;

import java.util.List;

/**
 * 字段spinner的adapter
 * Created by li on 2016/9/29.
 */
public class FieldAdapter extends BaseAdapter {


    private Context mContext;
    private List<?> list;

    public FieldAdapter(Context context, List<?> list) {
        this.mContext = context;
        this.list = list;
    }


    @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){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.map_suggestion_item,parent);
            holder = new ViewHolder();
            holder.fieldNameTv = convertView.findViewById(R.id.map_suggestion_item_address);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Object object = list.get(position);
        String name = "";
        if (object != null){
            if (object instanceof Field){
                Field field = (Field) object;
                name = field.getAlias();
            }else if (object instanceof ConfigAll.ResultBean.FieldsBean){
                ConfigAll.ResultBean.FieldsBean fieldsBean = (ConfigAll.ResultBean.FieldsBean) object;
                name = fieldsBean.getAlias();
            }else {
                name = object.toString();
            }
        }
        holder.fieldNameTv.setText(name);
        return convertView;
    }



    class  ViewHolder{
        TextView fieldNameTv;
   }
}
