Android基础 —— 在ListView中的Item 中添加Button控件实现点击事件

很多时候在android开发中要在ListView中的Item中添加Button ,ImageButton 等控件,在自定义的Adapter 中对Item中的控件添加点击事件并获取到该点击的Item中的数据时十分必要的。

 

以下是在自定义Adapter中对Button控件的OnClick 事件的添加和在Button的点击事件中对Item中的数据进行获取示例。

说明:以下方法在Item中对控件添加点击事件后ListView中的OnItemClick 方法将不再生效。

MainActivity中的实现

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener 
{
	private ListView mListView;
	private List<String> mListMes;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mListMes = new ArrayList<String>();
		mListMes.add("中国");
		mListMes.add("美国");
		mListMes.add("德国");
		mListMes.add("英国");
		
		setUpView();
	}

	private void setUpView()
	{
		mListView = (ListView) this.findViewById(R.id.listview);
		mListView.setAdapter(new MyAdapter(this, R.layout.item, mListMes));
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id)
	{
		Toast.makeText(this, "MyItem", Toast.LENGTH_SHORT).show();
	}
}

自定义的Adapter:

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 自定义Adapter
 * @author CFOX
 *
 */
public class MyAdapter extends BaseAdapter 
{
	private List<String> mListMes; //绑定数据
	private int mResourse;			//布局文件
	private LayoutInflater mInflater;
	private Context mContext;
	
	public MyAdapter(Context context, int resourse, List<String> listMes)
	{
		this.mListMes = listMes;
		this.mContext = context;
		this.mResourse = resourse;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount()
	{
		return mListMes.size();
	}

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

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		TextView textMes = null;
		Button mItemBtn1 = null;
		Button mItemBtn2 = null;
		if(convertView == null)
		{
			convertView = mInflater.inflate(mResourse, null);
			textMes = (TextView) convertView.findViewById(R.id.textmes);
			mItemBtn1 = (Button) convertView.findViewById(R.id.item_btn1);
			mItemBtn2 = (Button) convertView.findViewById(R.id.item_btn2);
			
			ViewCatch viewCatch = new ViewCatch();
			viewCatch.textMes = textMes;
			viewCatch.mItemBtn1 = mItemBtn1;
			viewCatch.mItemBtn2 = mItemBtn2;
			convertView.setTag(viewCatch);
		}
		else
		{
			ViewCatch viewCatch = (ViewCatch) convertView.getTag();
			textMes = viewCatch.textMes;
			mItemBtn1 = viewCatch.mItemBtn1;
			mItemBtn2 = viewCatch.mItemBtn2;
		}
		
		//第一中在ListView中添加button等控件的点击事件
		mItemBtn1.setOnClickListener(new MyButtonOnClickListener(position));
		//第二中在ListView中添加button等控件的点击事件,此方法无法获取到Item中的内容
		mItemBtn2.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v)
			{
				Toast.makeText(mContext, "button2", Toast.LENGTH_SHORT).show();
			}
		});
		
		textMes.setText(mListMes.get(position));
		return convertView;
	}

	private final class MyButtonOnClickListener implements Button.OnClickListener
	{
		private int position;
		public MyButtonOnClickListener(int position)
		{
			this.position = position;
		}

		@Override
		public void onClick(View v)
		{
			Toast.makeText(mContext, mListMes.get(position), Toast.LENGTH_SHORT).show();
		}
	}
	
	public final class ViewCatch
	{
		public TextView textMes;
		public Button mItemBtn1;
		public Button mItemBtn2;
	}
}

运行效果:



你可能感兴趣的:(android,ListView,button,Android基础)