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.ImageView;
import android.widget.TextView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.MainModule;
import java.util.List;

/**
 * Created by zombiexq on 2016/6/15.
 */
public class GridViewAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater inflater;
    private List<MainModule.MainModuleBean> gridviewlist;

    public  class ViewHolder {
        public ImageView iv;
        public TextView tv;
    }

    public GridViewAdapter(Context context, List<MainModule.MainModuleBean> gridviewlist) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.gridviewlist = gridviewlist;
    }

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

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

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

    public void setItemText(Class aclass, int txtId){
        for (MainModule.MainModuleBean moduleBean : gridviewlist){
            if (moduleBean != null){
                if (moduleBean.getAclass().getName().equals(aclass.getName())){
                    moduleBean.setTitle(txtId);
                    notifyDataSetChanged();
                    break;
                }
            }
        }
    }


    @Override
    public View getView(int p, View v, ViewGroup parent) {
        ViewHolder viewHolder;
        // if cached expired
        if (v == null) {
            v = inflater.inflate(R.layout.gridview_item, null);
            viewHolder = new ViewHolder();
            viewHolder.iv= v.findViewById(R.id.gridview_item_iv);
            viewHolder.tv= v.findViewById(R.id.gridview_item_tv);
            v.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) v.getTag();
        }
        viewHolder.tv.setText(gridviewlist.get(p).getTitle());
        viewHolder.iv.setImageResource(gridviewlist.get(p).getImage());
        return v;
    }
}
