Skip to content Skip to sidebar Skip to footer

Onitemclicklistener Vs Onclicklistener In Listview Android

This is gonna be long due to code. So I have a question about OnItemclickListener vs OnclickListener,and here I have two codes one for each and both worked, so is there any prefere

Solution 1:

I think setOnItemClickListener() is the best than onClickListener() because,

  1. It give you a single row click in activity or fragment where ListView defined.
  2. It is easier when you want to perform operation on onItemClickListener() such as open another activity or fragment.
  3. We can get selected row List object using view.getTag() method in on item click method.
  4. When you have call data from web api like soap or restful on listview item click then it is easy way to implement it.

Solution 2:

Adapter

The setOnItemClickListener is the preferred way of attaching the click listener on the items of the adapter as mentioned by the first comment above.

View

In your second scenario, where you have setOnClickListener attached on individual item inside the getView method which is also valid because the ItemView in this case is an indirect subclass on View on which setOnClickListener originally declared.

Post a Comment for "Onitemclicklistener Vs Onclicklistener In Listview Android"