3

I'm sorry for this silly question, but I'm new to Java, I would like to know what does this syntax mean and where I can obtain more information about it:

    (...)
    //I would like to know what does this syntax mean
    btvisualizar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {   
           // some code here               
        }
    });
    (...)

I got it from this topic: http://www.portalandroid.org/comunidade/viewtopic.php?f=2&t=9673

I think it is something related to "lambda expressions" that we have in C#, is that the same thing?

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
Cleiton
  • 17,663
  • 13
  • 46
  • 59

5 Answers5

5

That is an anonymous inner class. You can use it instead of this code:

btvisualizar.setOnClickListener(new MyClickListener());

And here you ClickListener class:

public class MyClickListener implements View.OnClickListener {
        public void onClick(View v) {   
           // some code here               
        }
}
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
3

As the others point, out, this is an anonymous inner class. It's syntactic shorthand that creates a new subclass of View.OnClickListener, one that overrides the onClick() method with the behavior you want.

Your intuition that this is related to a C# lambda expression is fairly accurate. Although Java doesn't have lambdas (yet), you can simulate one by creating an object with an 'apply' method and passing around a reference to it. This technique used in functional programming libraries for Java. Here's an example from Functional Java:

import fj.F;  
import fj.data.Array;  
import static fj.data.Array.array;  
import static fj.data.List.fromString;  
import static fj.function.Characters.isLowerCase;  

public final class Array_exists {  
  public static void main(final String[] args) {  
    final Array<String> a = array("Hello", "There", "what", "DAY", "iS", "iT");  
    final boolean b = a.exists(new F<String, Boolean>() {  
      public Boolean f(final String s) {  
        return fromString(s).forall(isLowerCase);  
      }  
    });  
    System.out.println(b); // true ("what" provides the only example; try removing it)  
  }  
} 

Instead of a View.OnClickListener you create a new F, which has an apply method called f instead of onClick().

See also Functional Programming in Java

Community
  • 1
  • 1
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
  • @Cleiton, you're welcome! The Functional Java site has both Java 1.5 examples (http://functionaljava.org/examples/1.5/) and examples using the Java 7 BGGA Closures proposal (http://functionaljava.org/examples/). – Jim Ferrans Apr 17 '11 at 21:17
1

Its called an anonymous inner class. Refer http://download.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
Umesh K
  • 13,436
  • 25
  • 87
  • 129
1

OnClickListener? I presume that onClick in the passed anonymous class instance is called whenever the user clicks on the visualiser, passing in the visualiser view instance.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
0

I am no java pro, but for me to break it down in easy to understand terms:

Im assuming btvisualizar is a clickable object in your code such as a button and you have already mapped it to a button or whatever it may be in your layout.

btvisualizar.setOnClickListener(new View.OnClickListener() {
   //This is basically telling the compiler that you want to make btvisualizar, when clicked on, do something, which you will tell it what to do below.
public void onClick(View v) { 
    //This begins the OnClick statement, anything you put between { and } here is your code as for what to do when clicked on              
    }
});
Warlock
  • 162
  • 6