GilbertLee
GilbertLee

Reputation: 696

Android setOnItemClickListener vs setOnClickListener

As I know, there are two methods to handle click on different list items:

  1. Use setTag() to set types for items of list in Adapter, then setOnItemClickListener() for the list and getTag() of the view to differentiate the type, like this:

listview.setOnItemClickListener(new OnItemClcikListener(){});

  1. Inside the adapter, setOnClickListener() individually for each item during getView(), like this:

item.setOnClickListener(new OnClickListener() {});

What is the difference and which one is preferred?

Upvotes: 6

Views: 2651

Answers (2)

Praveena
Praveena

Reputation: 6980

OnItemClickListener is very easy to manage compare to OnClickListener. If you still would love to manage OnClickListener I will tell why OnItemClickListener is much better than OnClickListener.

Once you start scrolling ListView items will start getting reused and you ended up creating lot of OnClickListener. Don't worry, this is not a memory leak as GC will come into picture and collect those but you should not feel safe also because GC pauses your activity even if it is fraction of second that's considerable.

So would I go with OnItemClickListener unless you planned something different for individual list item.

If you need to create specific portions of each item to be clickable or want more than one action to be performed for a given item, it would be best if possible to collect those actions into a single OnClickListener that is created once and then attached to each item in getView(). You can differentiate which item was clicked by attaching metadata about the click action and maybe list position to the views themselves with setTag().

Upvotes: 3

MineConsulting SRL
MineConsulting SRL

Reputation: 2340

I'm not sure i really understood what do you mean but i will try to give you a reply.

Use setTag() to set types for items of list in Adapter, then setOnItemClickListener() for the list and getTag() of the view to differentiate the type, like this: listview.setOnItemClickListener(new OnItemClcikListener(){});

You can use setTag() for set an Object like a tag, it means you can use it to put some informations into your cell view (ex: text, id and so on). To "differentiate the type" of your view i would suggest you to use `getViewTypeCount().

inside the adapter, setOnClickListener() individually for each item during getView(), like this: item.setOnClickListener(new OnClickListener() {});

This actually depends more on what you want to do with your list, if the behaviour of the click is strictly related to the informations which belong to your adapter or for example if you have a button inside your cell view, then set a listener for the cell view inside the adapter could be a good solution. But except for the last case i would say the first is the best solution, since you can put everything you want into your tag, and it gives you the chance to manage your list's click from your main Activity or Fragment.

Upvotes: 0

Related Questions