package com.martn.godutch.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.martn.godutch.R;

/**
 * Created MartnLei on 2015/1/16.
 */
public class AdapterMainGradView extends BaseAdapter {
    //定义一个实体，存放复杂对象
    private class Holder {
        ImageView imageView;
        TextView textView;
    }

    private Context mContext;
    //图片icon显示数组资源
    private Integer[] mImageRes = {
            R.drawable.grid_payout,
            R.drawable.grid_bill,
            R.drawable.grid_report,
            R.drawable.grid_account_book,
            R.drawable.grid_category,
            R.drawable.grid_user,
    };
    //文字显示资源
    private String[] mTextRes = new String[6];

    public AdapterMainGradView(Context context) {
        mContext = context;
        //实例化获取文字显示资源
        mTextRes[0] = mContext.getString(R.string.grid_payout);
        mTextRes[1] = mContext.getString(R.string.grid_bill);
        mTextRes[2] = mContext.getString(R.string.grid_report);
        mTextRes[3] = mContext.getString(R.string.grid_account_book);
        mTextRes[4] = mContext.getString(R.string.grid_category);
        mTextRes[5] = mContext.getString(R.string.grid_user);

    }

    @Override
    public int getCount() {
        return mImageRes.length;
    }

    @Override
    public Object getItem(int posi) {
        return null;
    }

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

    @Override
    public View getView(int posi, View convertView, ViewGroup viewGroup) {
        //构造view
        Holder holder;
        View v;
        if(convertView == null) {
            //
            holder = new Holder();
            //实例化布局view，需要在每一项中显示的
            convertView = LayoutInflater.from(mContext).inflate(R.layout.main_body_item, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_mainbody_icon);
            holder.textView = (TextView) convertView.findViewById(R.id.tv_mainbody_text);
            convertView.setTag(holder);

        }else {
            //得到view组件的资源项的组合列表
            holder = (Holder)convertView.getTag();
        }
        //绑定资源
        holder.textView.setText(mTextRes[posi]);
        holder.imageView.setBackgroundResource(mImageRes[posi]);


        return convertView;
    }
}
