自定义listadapter每个列表项目上面各有一个按钮,求这些按钮响应事件的方法
问题已经解决,代码如下
class EpisodeAdapter extends ArrayAdapter implements OnClickListener{
public EpisodeAdapter(Context ctx,String[] episodes){
super(ctx,R.layout.adapter_episode,episodes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO: Implement this method
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.adapter_episode, parent, false);
LayoutParams p=(parent.getLayoutParams());
p.width=LayoutParams.FILL_PARENT;
parent.setLayoutParams(p);
String text = getItem(position);
Button adptxt=(Button)view.findViewById(R.id.adp_episode_tv);
adptxt.setTag(position);
adptxt.setOnClickListener(this);
((ImageButton)view.findViewById(R.id.adp_btn_remove)).setOnClickListener(this);
((ImageButton)view.findViewById(R.id.adp_btn_rename)).setOnClickListener(this);
((ImageButton)view.findViewById(R.id.adp_btn_move)).setOnClickListener(this);
((ImageButton)view.findViewById(R.id.adp_btn_remove)).setTag(position);
((ImageButton)view.findViewById(R.id.adp_btn_move)).setTag(position);
((ImageButton)view.findViewById(R.id.adp_btn_rename)).setTag(position);
adptxt.setText(text);
return view;
}
@Override
public void onClick(View p1)
{
// TODO: Implement this method
Ut.tw(this.getContext(),"Clicked "+p1.getId()+" at "+p1.getTag());
}
}
在adapter的getView方法中设置就行拉
可以在adapter里写出点击事件啊,不过这样的代码好像不太规范但不影响应用,或者是像楼上所说在adapter 先获取位置再getView方法获取view,在写点击事件
问题已经解决,在getview时设置事件监听并settag,在点击事件中gettag