在安卓中,按钮点击事件(这个点击事件是在适配器中,通过viewHolder)
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHold viewHold = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.item, null);
viewHold = new ViewHold();
viewHold.logo = (ImageView) convertView.findViewById(R.id.logo);
viewHold.title = (TextView) convertView.findViewById(R.id.title);
viewHold.version = (TextView) convertView.findViewById(R.id.version);
viewHold.size = (TextView) convertView.findViewById(R.id.size);
viewHold.btn = (Button) convertView.findViewById(R.id.btn);
convertView.setTag(viewHold);
} else {
viewHold = (ViewHold) convertView.getTag();
}
//ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
//TextView title = (TextView) convertView.findViewById(R.id.title);
//TextView version = (TextView) convertView.findViewById(R.id.version);
//TextView size = (TextView) convertView.findViewById(R.id.size);
Map map = list.get(position);
viewHold.logo.setImageResource((Integer) map.get("logo"));
viewHold.title.setText((String) map.get("title"));
viewHold.version.setText((String) map.get("version"));
viewHold.size.setText((String) map.get("size"));
<span style="color:#FF0000;">viewHold.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
control.onBtnClick(position);
}
});</span>
return convertView;
}
在主mainactivity中实现这个接口方法:
@Override
public void onBtnClick(int position) {
Toast.makeText(this, "item:" + position, Toast.LENGTH_SHORT).show();
}
Icontrol是一个借口类 在MyAdapter中调用接口。