标签:android style blog io color ar os java sp
ListView lstView = (ListView) this.findViewById(R.id.listView1); SimpleAdapter adapter=new SimpleAdapter(context, data, resource, from, to); lstView.setAdapter(adapter);
OnItemClickListener mItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } }; listView.setOnItemClickListener(mItemClickListener);
对于Button ,ImageButton这样的按钮控件实现单事件,接下来我们看看如何实现这样的功能:
继承SimpleAdapter进行重写getView方法
为什么不继承BaseAdapter适配器进行处理,因为BaseAdapter是一个非常基础的基类,对于一般的TextViwe ,ImageView,Button控件的数据绑定都没有实现
java代码:
private class MySimpleAdapter extends SimpleAdapter { @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v = super.getView(position, convertView, parent); ImageButton btn=(ImageButton) v.findViewById(R.id.icon); btn.setTag(position); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mDataList.remove((int)v.getTag()); notifyDataSetChanged(); Toast.makeText(getApplicationContext(), "µ¥»÷ÎÒÁË"+v.getTag(), 1).show(); } }); return v; } public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); // TODO Auto-generated constructor stub } }
当单击某个按钮时,按钮事件需要知道单击的是ListView的哪一项,可以为每个Button对象添加一个Tag,我们这里用listview的项position的索引作为tag
Android SimpleAdapter 中添加按钮响应事件,getView的重写
标签:android style blog io color ar os java sp
原文地址:http://www.cnblogs.com/AminHuang/p/4088490.html