Reputation: 21893
Right now I have a listview with a custom adapter extending ArrayAdapter
. I have the list item on click method inside getView()
But I can't help wondering if it's better to put it in the activity class like the following myList.setOnClickListener()
Does it make a difference as far as perfomance?
Upvotes: 2
Views: 1175
Reputation: 3522
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.
Upvotes: 1
Reputation: 14022
I believe that it is better that set listener for items in getView()
.
If you set a listener in getView()
of adapter,then each item can respond to the event,direct,independent and even in difference manner of other items.So you can set different listeners that extend different classes to respond to the event.But to avoid of decreasing of performance,set listener for your item if and only if it is not created yet. You can do that with the well know method of View Holders. Here are an example
Upvotes: 1
Reputation: 4591
If the actions of the listener are closely related to the contents of the list, do everything in the getView(), it'll make it easier to re-use and keeps related responsibilities together.
If however the actions performed by the listener are more to do with the activity or fragment as a whole, define and set the listener outside the getView method for the same reason. Its just like OO encapsulation and assignment of responsibilities in miniature...
Upvotes: 0
Reputation: 3441
It would be better to do it the second way. Although you won't notice a difference unless you have a lot of items. So I would do it just in case.
Upvotes: 0