package com.chaoran.component;

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

import com.chaoran.db.DBManager;
import com.chaoran.lx.activity.DownDataActivity;
import com.activity.chaoran.R;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ButAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	public ArrayList list;
	// private String filedName;
	private DownDataActivity context;
	private ProgressDialog pd;
	private DBManager dbManager;
	// private Handler handler;
	private String tableName;
    private String showFilde;
	public ButAdapter(DownDataActivity context, ArrayList list,
			ProgressDialog pd, DBManager dbManager, String tableName,String showFilde) {
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.list = list;
		this.context = context;
		this.pd = pd;
		this.dbManager = dbManager;
		// this.handler = handler;
		this.tableName = tableName;
		this.showFilde=showFilde;
	}

	@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;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final HashMap map = (HashMap) list.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.butadapter_list_child,
					null, false);
		}
		final TextView tablename = (TextView) convertView
				.findViewById(R.id.tablename);
		String table = map.get(tableName).toString();
		Object ob=map.get(showFilde);
		String lb ="";
		if(ob!=null){
			lb=ob.toString();
		}
		if (dbManager.existsTable(table.toUpperCase()) > 0) {
			lb = lb.concat("(").concat(String.valueOf(dbManager.rsCount(table)))
					.concat(")");
		}
		tablename.setText(lb);
		Button button = (Button) convertView.findViewById(R.id.downbut);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				context.downData(map, tablename);
			}
		});
		return convertView;
	}

	public void clean() {
		inflater = null;
		context = null;
		list = null;
		// filedName=null;
		pd = null;
		// handler = null;
		dbManager = null;
	}

}
