android 中实现列表中多个控件的点击事件

前几天做项目的时候一直苦恼于如何实现列表中多个控件的点击事件,经人指教后来才发觉有这么一个方法。现在写在这里一是方便自己以后查看 二是为其他的跟我一样的困于这个难题的朋友提供个方法,如果大家有更好的也可以给我留评论,我会去试试看的。
我的实现列表中的多个控件的点击事件主要是在adapter中实现的,在getview()方法中将convertVIEW.setTag(holder),再将要实现的点击事件的控件setTag()到holder里。废话不多说 上代码。
package view;

import java.util.List;

import com.alibaba.fastjson.JSONObject;

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


public class MineAddressListAdapter extends BaseAdapter {

	private Context myContext;
	
	private int mLeftWidth=0;
	
	private List datas;
	
	public MineAddressListAdapter(Context context)
	{
		this.myContext=context;
	}
	
	public void setData(List data){
		this.datas=data;
		notifyDataSetChanged();
	}
	

	@Override
	public int getCount() {
		return datas==null?0:datas.size();
	}

	@Override
	public Object getItem(int position) {
		
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemHolder holder=null;
		if(convertView==null) {
			convertView=LayoutInflater.from(myContext).inflate(R.layout.mine_address_item, null);
			holder=new ItemHolder();
			holder.consignee = (TextView) convertView.findViewById(R.id.mine_address_consignee);

			holder.district = (TextView) convertView.findViewById(R.id.mine_address_district);
			holder.address = (TextView) convertView.findViewById(R.id.mine_address_address);
			convertView.setTag(holder);
		}
		else{
			holder=(ItemHolder)convertView.getTag();
		}
			JSONObject obj = (JSONObject) this.getItem(position);
			holder.consignee.setText(obj.getString("CONSIGNEE"));
			String province = obj.getString("PROVINCE_NAME");
			String city = obj.getString("CITY_NAME");
			String disrict = obj.getString("DISTRICT_NAME");
			holder.district.setText(province+city+disrict);
			holder.address.setText(obj.getString("ADDRESS"));
			return convertView;
		}
	
	private class ItemHolder
	{
		private int position;
		private TextView consignee;
		private TextView mobile;
		private TextView district;
		private TextView address;
		private TextView zipcode;
	}


	private OnClickListener ConsigneeListener =new OnClickListener (){
		@overdride
			public void onClick(View view){
					ItemHolder holder = (ItemHolder) view.getTag();
					JSONObject data = (JSONObject) this.getItem(handler.position);
					String name = data.getString("NAME");
					Intent intent = new Intent(myContext,NameActivity.class);
					Bundle bundle = new Bundle();
					bundle.put("Name",name);
					intent.putExtras(bundle);
					myContext.startActivity(intent);
		}
	} 
		private OnClickListener MobileListener =new OnClickListener (){
		@overdride
			public void onClick(View view){
					ItemHolder holder = (ItemHolder) view.getTag();
					JSONObject data = (JSONObject) this.getItem(handler.position);
					String mobile = data.getString("MOBILE");
					Intent intent = new Intent(myContext MobileActivity.class);
					Bundle bundle = new Bundle();
					bundle.put(" Mobile",mobile);
					intent.putExtras(bundle);
					myContext.startActivity(intent);
		}
	} 




你可能感兴趣的:(android)