package com.lianxin.pubqq.activity.adpter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.list.bean.UserNode;
import com.lianxin.panqq.list.utils.AddressUtils;
import com.lianxin.panqq.list.adpter.ExListAdapter;
import com.lianxin.panqq.utils.ImagesUtils;

public class UserListAdapter extends ExListAdapter{

	private List<UserNode> mList;	
	private OnMyButClickListener ButClickListener; 

	public UserListAdapter(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public UserListAdapter(Context context,List<UserNode> userList) {
	    super(context);	
	    this.mList=userList;

    }

	@Override
	public void setData(Object object) {
		this.mList = (List<UserNode>)object; 
	}    
    
	@Override
	public int getCount() {
		return mList==null?0:mList.size();
	}

	@Override
	public UserNode getItem(int position) {
		return mList.get(position);
	}

 
	public String getUser(int position) {
		return mList.get(position).getName();
	}
	
	 @Override
	public long getItemId(int position) {

		return position;		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_userlist, null);
			holder.avator=(ImageView)convertView.findViewById(R.id.avator);
			holder.name=(TextView)convertView.findViewById(R.id.name);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.command = (Button) convertView.findViewById(R.id.command);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final UserNode bean = getItem(position);

		int imageid = bean.imageid%80;
		holder.avator.setImageResource(ImagesUtils.images[imageid]);
		//holder.avator.setImageResource(ImagesUtils.images[5]);		
		String strname = bean.getName()+"("+bean.getUserId()+")";
		String straddress = bean.straddress;
		if(bean.address>0)
			straddress = AddressUtils.getAddress(bean.address);			
		//String strContent = ""+(position+1)+","+bean.sex+","+bean.age+"岁,"+getAddress(bean.address);
		String strContent = ""+(position+1)+","+bean.sex+","+bean.age+"岁,"+straddress;		
		holder.name.setText(strname);
		holder.content.setText(strContent);

		// 设置点击事件
		final int pos = position;			
		holder.command.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 同意别人发的好友请求
                //Toast.makeText(mContext, "您点击了-" , Toast.LENGTH_LONG).show();
                if(ButClickListener != null){ 
            	     ButClickListener.onButClicked(pos);                          	
                }                     
			}
		});		
		//判断当前故事是否是自己的
		final int pposition = position;

		return convertView;
	}

	private static class ViewHolder {

		public TextView name;
		public ImageView avator;
		TextView content;
		Button command;
	}


    public static interface OnMyButClickListener{  
        public void onButClicked( int position );        
    }	

    public void setButListener(OnMyButClickListener onButClickListener) {  
        this.ButClickListener = onButClickListener;  
    }

}
