• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is this method doing?  RSS feed

 
Chuck Mondi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am somewhat new to Java and am exploring Android development as it peaks my interest. In the book Pro Android 2 Development, the author uses a snippet of code below. I don't however understand why he is implementing the code the way he does. I don't know why new onKeyListener with the onKey method is being passed to the setOnKeyListener method. I can see passing an interface object, but why an interface object and its implemented method?

 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can see :

This method is returning a primitive boolean, so true or false

Firstly it checks to see if the event is ACTION_DOWN, presumably the down arror on the cursors, if it isn't ACTION_DOWN then it returns false and exits this method.

If it is ACTION_DOWN it continues to the next if statement, if the event is KEYCODE_DPAD_CENTER it continues, otherwise it exits that if statement and returns false

Otherwise, it adds the edit text into what appears to be a collection of some sort, then calls the notifyDataSetChanged, then sets the edit text to blank
 
pete reisinger
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
this is like annonymous class, it doesn't have a reference.
It is useful in this case, because the passed object is interface, so you don't have to code new class, that implements interface, then create an instance of that class and pass it, but all you do is to
pass this annonymous class. You save coding, plus you don't need reference to the passed object.
you can understand it as new object that implements that interface, taht's why there are implemented methods that are in the interface.

I hope I explained it a bit.
 
Chuck Mondi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete,
I believe you answered my question that it is an anonymous class.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!