It is better to put it on the list rather then on single views. The getView method might be called more than once for each view (often it is) and then you'd call the method setOnClickListener more than once for each view of the list (uselessly).
It is surely better to call it just once on the list and then identify each time the clicked view.
A small example is the following.
You set the listener on the ListView (or on a generic ViewGroup):
// this one may be called just once
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {
// you have the position of the clicked item, then you know what item has been clicked and you can do some stuff related to that one
}
});
You set the listener on each item of the ViewGroup:
// in the adapter class you have the getView method
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
LinearLayout myLayout = (LinearLayout) mContext.getLayoutInflater().inflate(R.layout.something, null);
//this one is called AT LEAST ONCE on each element of the list...
myLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Do something
}
});
return myLayout;
}
You can see the difference! If you don't have to specialize the behaviour of the application for each view of the ViewGroud (ListView, GridView, etc.) surely the first alternative is better!
Remember that, while creating the interface, the getView method is often called more than once for each element. Then performances are higher if you do fewer operations... furthermore, the first solution is clearer for me.