package com.chaoran.component;

import java.util.ArrayList;
import java.util.HashMap;

import com.chaoran.entiry.Zujian;
import com.activity.chaoran.R;
import com.util.DialogUtil;
import com.util.SysUtil;

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

public class MyAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ArrayList list;
	public int cur_pos = -1;
	private String filedName;
    private Context context;
	public MyAdapter(Context context, ArrayList list, String filedName) {
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.list = list;
		this.filedName = filedName;
		this.context=context;
	}

	@Override
	public int getCount() {
		return this.list != null ? this.list.size() : 0;
	}

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

	@Override
	public long getItemId(int position) {
		return position;
	}
	public void clear(){
		inflater=null;
		if(list!=null){
			list.clear();
			list=null;
			context=null;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Zujian zj = null;
		if (convertView == null) {
			// System.out.println("创建新的对象-------------------------------");
			zj = new Zujian();
			convertView = inflater.inflate(R.layout.list_child, null, false);
			zj.tv = (TextView) convertView.findViewById(R.id.list_child_text);// 显示文字
			convertView.setTag(zj);
		} else {
			zj = (Zujian) convertView.getTag();
		}
		HashMap map = (HashMap) list.get(position);
		zj.tv.setText(map.get(filedName).toString());
		if (position == cur_pos) {// 如果当前的行就是ListView中选中的一行，就更改显示样式
			convertView.setBackgroundColor(Color.LTGRAY);// 更改整行的背景色
			zj.tv.setTextColor(Color.RED);// 更改字体颜色
		} else {
			try {
				int bcolor = Color.WHITE;
				if (map.containsKey("__BCOLOR")) {
					String colorValue = ((String) map.get("__BCOLOR"));
					if (colorValue != null && colorValue.trim().length() > 0) {
						bcolor = SysUtil.toColor(Integer.decode(colorValue
								.trim()));
					}
				}
				convertView.setBackgroundColor(bcolor);// 更改整行的背景色
				int fcolor = Color.BLACK;
				if (map.containsKey("__FCOLOR")) {
					String fcolorValue = ((String) map.get("__FCOLOR"));
					if (fcolorValue != null && fcolorValue.trim().length() > 0) {
						fcolor = SysUtil.toColor(Integer.decode(fcolorValue
								.trim()));
					}
				}
				zj.tv.setTextColor(fcolor);// 更改字体颜色
			} catch (NumberFormatException exception) {
				DialogUtil.builder(context, "错误信息",exception.toString(),0);
			}
		}
		return convertView;
	}
}