package edu.bupt.mms.ui;

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

import edu.bupt.mms.R;
  
import android.content.Context;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.CheckBox;  
import android.widget.ImageView;
import android.widget.TextView;  
  
public class AgendaMultiDeleteAdapter extends BaseAdapter {  
    // å¡«åæ°æ®çlist  
    private ArrayList<Map<String,String>> list;  
    // ç¨æ¥æ§å¶CheckBoxçé?ä¸­ç¶å? 
    private static HashMap<Integer, Boolean> isSelected;  
    // ä¸ä¸æ? 
    private Context context;  
    // ç¨æ¥å¯¼å¥å¸å±  
    private LayoutInflater inflater = null;  
  
    // æé?å? 
    public AgendaMultiDeleteAdapter(ArrayList<Map<String, String>> list, Context context) {  
        this.context = context;  
        this.list = list;  
        inflater = LayoutInflater.from(context);  
        isSelected = new HashMap<Integer, Boolean>();  
        // åå§åæ°æ? 
        initDate();  
    }  
  
    // åå§åisSelectedçæ°æ? 
    private void initDate() {  
        for (int i = 0; i < list.size(); i++) {  
            getIsSelected().put(i, false);  
        }  
    }  
  
    @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 = null;  
        if (convertView == null) {  
            // è·å¾ViewHolderå¯¹è±¡  
            holder = new ViewHolder();  
            // å¯¼å¥å¸å±å¹¶èµå¼ç»convertview  
            convertView = inflater.inflate(R.layout.agendamultideleteadapter, null);  

            holder.iv = (ImageView) convertView.findViewById(R.id.bluebt);
            holder.tv = (TextView) convertView.findViewById(R.id.item_tv);  
            holder.tt = (TextView) convertView.findViewById(R.id.item_tt); 
            holder.yy = (TextView) convertView.findViewById(R.id.item_yy); 
            holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);  
            // ä¸ºviewè®¾ç½®æ ç­¾  
            convertView.setTag(holder);  
        } else {  
            // ååºholder  
            holder = (ViewHolder) convertView.getTag();  
        }  
        // è®¾ç½®listä¸­TextViewçæ¾ç¤? 
        
        holder.iv.setImageDrawable(context.getResources().getDrawable(R.drawable.bluebt));
        holder.tv.setText(list.get(position).get("title"));  
        holder.tt.setText(list.get(position).get("time"));
        holder.yy.setText(list.get(position).get("year"));  
        // æ ¹æ®isSelectedæ¥è®¾ç½®checkboxçé?ä¸­ç¶å? 
        holder.cb.setChecked(getIsSelected().get(position));  
        return convertView;  
    }  
  
    public static HashMap<Integer, Boolean> getIsSelected() {  
        return isSelected;  
    }  
  
    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {  
        AgendaMultiDeleteAdapter.isSelected = isSelected;  
    }  
  
    public static class ViewHolder {  
    	ImageView iv;
        TextView tv;  
        TextView tt;
        TextView yy;
        CheckBox cb;  
    }  
}  