package com.tofly.ghrq_route.common.ui.adapter;

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

import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.data.GeodatabaseFeatureTable;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.map.domain.ConfigAll;

import java.util.List;

/**
 * Created by li on 2016/9/29.
 */
public class TableNameAdapter extends BaseAdapter {
    private List<?> list;
    private Context mContext;

    public TableNameAdapter(List<?> list, Context context) {
        this.list = list;
        this.mContext = 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) {
            convertView = View.inflate(mContext, R.layout.map_suggestion_item, parent);
            holder = new ViewHolder();
            holder.tableNameTv = 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 FeatureTable){
                GeodatabaseFeatureTable geodatabaseFeatureTable = (GeodatabaseFeatureTable) object;
                name = geodatabaseFeatureTable.getTableName();
            }else if (object instanceof ConfigAll.ResultBean){
                ConfigAll.ResultBean resultBean = (ConfigAll.ResultBean) object;
                name = resultBean.getLayername();
            }else {
                name = object.toString();
            }
        }
        holder.tableNameTv.setText(name);

        return convertView;
    }

    private class ViewHolder {
         TextView tableNameTv;
    }

}
