3

I searched the internet and read out documents on Google Android Help Centre, But still now I am not clear what the difference between the two and when I will use it at what situation? I go through stack-overflow not found any detailed answer.

serviceListViewProviderPage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                      //something to do
        }
    });

and

serviceListViewProviderPage.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//something to do
}

Thanks in Advance

Kaushik
  • 6,150
  • 5
  • 39
  • 54
Ramanuj Basu
  • 77
  • 3
  • 15
  • 4
    Do you know what an interface is? – Tim Nov 10 '16 at 12:46
  • You can read more in [Developers android](https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html) Basically, `setOnItemClickListener` is a method that waits user touch. In this case, into an adapter, has a list of itens. That's why has a method called `onItemClick`. This method has the position of the list item. – Rodrigo Paixão Nov 10 '16 at 12:51
  • Get up to speed on interfaces: https://stackoverflow.com/documentation/java/102/interfaces – denvercoder9 Nov 10 '16 at 12:56

3 Answers3

3

Both are same but different declarations and uses. First, lets see what we are doing.

Here:

view.setOnItemClickListener(Listener);

You are setting a listener in your view.

After, you must override the method onItemClick of the OnItemClickListener interface in order to follow the contract provided and make an action on item click.


Now see your code examples:

FIRST CASE

// set a listener to your wiew                 
serviceListViewProviderPage.setOnItemClickListener(
      // create a new OnItemClickListener 
      new AdapterView.OnItemClickListener() {

    @Override
    // 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  //something to do
    }
});

Here you're declaring the listener as a anonymous inner class at time you set it to your view.

PROS:

  • fast to code

CONS:

  • if the logic inside the metod is too long or the interface has many methods you will loose readability
  • you cannot reuse the logic inside the Listener
  • can cause memory leaks (thanks to @Murat K)

SECOND CASE

To understand second one you must see the code MUST be inside a View that implements AdapterView.OnItemClickListener, that's why you can use this

// here you set the class itself as a listener
serviceListViewProviderPage.setOnItemClickListener(this);

But, as long as you must follow the contract of the interface, the class must implement the method:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //something to do
}

PROS:

  • readability
  • reusability

CONS:

  • make a View be also a Listener is not my prefered way, I like more to have a class that is only a Listener and another is only a View.
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

OnItemClickListener is an interface and interface has a property that the class which implements it must implement its unimplemented method(s) (because interface contains declaration of methods not implementation).

"serviceListViewProviderPage.setOnItemClickListener(this);" In above statement you need to implement OnItemClickListener in your class and its unimplemented methods and providing your class reference as this reference as argument.

new AdapterView.OnItemClickListener() In above statement you are using anonymous class.

To better understand you should read interface, anonymous class concept, this reference.

Chirag Jain
  • 628
  • 11
  • 24
0

on the first case, you decide that your interface OnItemClickListener should be used as an anonymous implementation (meaning that you provide the code right then and there, for one time use in your project).

so in this case you define your interface implementation right when you call the method:

serviceListViewProviderPage.setOnItemClickListener(new AdapterView.OnItemClickListener() {....});

In the second case is even more simple:

Instead of writing all the code for one time anonymous use, you make the class containing the code implement the interface you want ( in this case the AdapterView.OnItemClickListener interface)

Therefore when you call the method you pass "this" as a parameter, because your class is implementing the interface required by the method This is shown by the fact that you have the method defined directly in the class:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//something to do
}

and because of that your class implements the interface and you can call:

serviceListViewProviderPage.setOnItemClickListener(this);

When and where to use each of these forms will be at the sofware developer's discretion, after evaluating how to better follow and use the best practice guidelines for software development

Hope this helps!

HenriqueMS
  • 3,864
  • 2
  • 30
  • 39