package com.lianxin.pubqq.main.adpter;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.BaseExpandableListAdapter;

import com.lianxin.pubqq.R;

public class ExListViewAdpter extends BaseExpandableListAdapter {
    private Context context;  
    private List<String> Group;  
    private List<List<String>> Child;
    
    public ExListViewAdpter(Context context, List<String> objects1, List<List<String>> objects2) {  
        this.context = context;  
        this.Group = objects1;
        this.Child = objects2;        
    }  
  
    @Override  
    public int getGroupCount() {  
        return Group.size();  
    }  
  
    @Override  
    public int getChildrenCount(int groupPosition) {  
        return Child.get(groupPosition).size();  
    }  
  
    @Override  
    public Object getGroup(int groupPosition) {  
        return Group.get(groupPosition);  
    }  
  
    @Override  
    public Object getChild(int groupPosition, int childPosition) {  
        return Child.get(groupPosition).get(childPosition);  
    }  
  
    @Override  
    public long getGroupId(int groupPosition) {  
        return groupPosition;  
    }  
  
    @Override  
    public long getChildId(int groupPosition, int childPosition) {  
        return childPosition;  
    }  
  
    @Override  
    public boolean hasStableIds() {  
        return true;  
    }  
    
    @Override  
    public View getGroupView(int groupPosition, boolean isExpanded,  
                             View convertView, ViewGroup parent) {  
        GroupHolder groupHolder = null;     //我们自己设定的一个简单类，用来存储控件的相关信息  
        if (convertView == null) {          //这里的convertView其实是一个起缓冲作用的，工具，因为当一个item从屏幕中滚出，我们把它放到convertView里                                                 //，这样再滑回来的时候可以直接去取，不用重新创建，这里也推荐一个网址，大家可以详细了解  
            convertView = LayoutInflater.from(context).inflate(  
                    R.layout.layout_item_group, null);      //把界面放到缓冲区  
            groupHolder = new GroupHolder();          //实例化我们创建的这个类  
            groupHolder.groupName=(TextView)convertView.findViewById(R.id.groupName);  //实例化类里的TextView  
            groupHolder.groupCount=(TextView)convertView.findViewById(R.id.groupCount);
            convertView.setTag(groupHolder);                                 //给view对象一个标签，告诉计算机我们已经在缓冲区里放了一个view，下回直                                                                               //接来拿就行了  
        } else {  
            groupHolder = (GroupHolder) convertView.getTag();     //然后他就直接来拿  
        }  
//        groupHolder.txt.setText(Group.get(groupPosition));//最后在相应的group里设置他相应的Text  
		String group=getGroup(groupPosition).toString();
		groupHolder.groupName.setText(group);
		int count=getChildrenCount(groupPosition);
		groupHolder.groupCount.setText("["+count+"]");		
        return convertView; 
    	        
    } 
    
    @Override  
    public View getChildView(int groupPosition, int childPosition,  
                             boolean isLastChild, View convertView, ViewGroup parent) {  
 
        ItemHolder itemHolder = null;  
        if (convertView == null) {  
            convertView = LayoutInflater.from(context).inflate(  
                    R.layout.layout_item_child, null);  
            itemHolder = new ItemHolder();  
            itemHolder.img=(ImageView)convertView.findViewById(R.id.contactIcon);   		
    		itemHolder.name=(TextView)convertView.findViewById(R.id.name);
    		itemHolder.description=(TextView)convertView.findViewById(R.id.description);

            convertView.setTag(itemHolder);  
        } else {  
            itemHolder = (ItemHolder) convertView.getTag();  
        }  
        itemHolder.img.setImageResource(R.drawable.head);
        itemHolder.name.setText(Child.get(groupPosition).get(childPosition));
        itemHolder.description.setText("10023");
        return convertView;      	
	    	
    }  
  
    @Override  
    public boolean isChildSelectable(int groupPosition, int childPosition) {  
        return true;  
    } 
 
	public static class GroupHolder {
		TextView groupName;  
		TextView groupCount;    		
	}    
    
	public static class ItemHolder {
		ImageView img;
		TextView name;
		TextView description;    		
	}

}