ListView和Button混合后,获取所点击button在ListView中的position

部分重要代码

   
public class lvButtonAdapter extends BaseAdapter {  
private class ButtonViewHolder {  
TextView tv_Number;
TextView tv_otheruser;
ImageButton bt_challenge;
int position;  
}  
private int mButtonClickPosition;  
private ArrayList<HashMap<String, Object>> mAppList;  
private LayoutInflater mInflater;  
private Context mContext;  
private String[] keyString;  
private int[] valueViewID;  
private ButtonViewHolder holder;  
  
public lvButtonAdapter(Context c, ArrayList<HashMap<String, Object>> appList, int resource,   
String[] from, int[] to) {  
mAppList = appList;  
mContext = c;  
mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
keyString = new String[from.length];  
valueViewID = new int[to.length];  
System.arraycopy(from, 0, keyString, 0, from.length);  
System.arraycopy(to, 0, valueViewID, 0, to.length);  
}  
  
@Override  
public int getCount() {  
return mAppList.size();  
}  
  
@Override  
public Object getItem(int position) {  
return mAppList.get(position);  
}  
  
@Override  
public long getItemId(int position) {  
return position;  
}  
  
public void removeItem(int position){  
mAppList.remove(position);  
this.notifyDataSetChanged();  
}  
  
@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
if (convertView != null) {  
holder = (buttonViewHolder) convertView.getTag();  
} else {  
convertView = mInflater.inflate(R.layout.lvitem, null);  
holder = new buttonViewHolder();  
  holder.tv_Number = (TextView)convertView.findViewById(R.id.tv_Number);
  holder.tv_otheruser = (TextView)convertView.findViewById(R.id.tv_otheruser);
  holder.bt_challenge = (ImageButton)convertView.findViewById(R.id.bt_challenge);
convertView.setTag(holder);  
}  
  
holder.position = position;//获取每个item位置  
HashMap<String, Object> appInfo = mAppList.get(position);  
if (appInfo != null) {  
String aname = (String) appInfo.get(keyString[1]);  
int mid = (Integer)appInfo.get(keyString[0]);  
int bid = (Integer)appInfo.get(keyString[2]);  
holder.appName.setText(aname);  
holder.appIcon.setImageDrawable(holder.appIcon.getResources().getDrawable(mid));  
holder.buttonClose.setImageDrawable(holder.buttonClose.getResources().getDrawable(bid));  
holder.buttonClose.setOnClickListener(new lvButtonListener(position));  
}   
return convertView;  
}  
  
public ButtonViewHolder getViewHolder(View v){  
  if (v.getTag() == null){  
    return getViewHolder((View) v.getParent());  
  }  
  return (buttonViewHolder ) v.getTag();  
}//注意此方法的使用老外写的  
class lvButtonListener implements OnClickListener {  
private int position;  
  
lvButtonListener(int pos) {  
position = pos;  
}  
  
@Override  
public void onClick(View v) {  
int vid=v.getId();  
if (vid == holder.buttonClose.getId()){  
ButtonViewHolder holder = getViewHolder(v);  
mButtonClickPosition = holder.position;//此处获得所点击button在list中的位置即:position    
//removeItem(position);  
}  
			        	if (mButtonClickPosition == 0) {
							 Bundle b = new Bundle ();
				             Intent intent = new Intent();		            	 
			            	 intent.setClass(A.this,B.class);
			            	 user2 = "用户" ; //+ id
			            	 b.putString("user1", user1);
			            	 b.putString("user2", user2);	//user2为接收来的用户
			            	 intent.putExtras(b);
			            	 startActivity(intent);
						}
			        	if (mButtonClickPosition >= 1){
							 Bundle b = new Bundle ();
				             Intent intent = new Intent();		            	 
			            	 intent.setClass(A.this,B.class);
			            	 user2 = "用户" + mButtonClickPosition; //+ id
			            	 b.putString("user1", user1);
			            	 b.putString("user2", user2);	//user2为接收来的用户
			            	 intent.putExtras(b);
			            	 startActivity(intent);
							}
}  
}  

  

posted on 2012-11-01 17:57  Code大蛇丸  阅读(1122)  评论(0编辑  收藏  举报