java - What is the difference between setOnItemClickListener and only onItemClick -
i searched internet , read out documents on google android centre
, still not clear difference between 2 , when use @ situation? go through stack-overflow not found detailed answer.
servicelistviewproviderpage.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { //something } });
and
servicelistviewproviderpage.setonitemclicklistener(this); public void onitemclick(adapterview<?> parent, view view, int position, long id) { //something }
thanks in advance
both same different declarations , uses. first, lets see doing.
here:
view.setonitemclicklistener(listener);
you setting listener in view.
after, must override method onitemclick
of onitemclicklistener
interface in order follow contract provided , make action on item click.
now see code examples:
first case
// set listener wiew servicelistviewproviderpage.setonitemclicklistener( // create new onitemclicklistener new adapterview.onitemclicklistener() { @override // public void onitemclick(adapterview<?> parent, view view, int position, long id) { //something } });
here you're declaring listener anonymous inner class @ time set view.
pros:
- fast code
cons:
- if logic inside metod long or
interface
has many methods loose readability - you cannot reuse logic inside
listener
- can cause memory leaks (thanks @murat k)
second case
to understand second 1 must see code must inside view
implements adapterview.onitemclicklistener
, that's why can use this
// here set class listener servicelistviewproviderpage.setonitemclicklistener(this);
but, long must follow contract of interface, class must implement method:
public void onitemclick(adapterview<?> parent, view view, int position, long id) { //something }
pros:
- readability
- reusability
cons:
- make
view
listener
not prefered way, more have classlistener
,view
.
Comments
Post a Comment