As you say, "this structure is kind of messy", and even the name for it may strike you as a little 'messy';) It is called an "anonymous class". You might want to look it up in the classic online tutorials originally created by Sun and now maintained by Oracle.
I found it messy too, and I still find the standard indentation of the anonymous class annoying. But I decided to learn to live with it because 1) it is extremely common, and that not just in Android Java 2) there is one big, good thing it does for us: it spares the reader from having to track down all references to the class to verify that it really is instantiated only in that one place. This really does simplify debugging. It also explains why it is heavily used in Swing and JavaFX as well as Android. In fact, that is pretty much what the above-mentioned tutorials says at http://docs.oracle.com/javase/tutorial/uiswing/events/generalrules.html#innerClasses. On Feb 22, 8:58 am, rhaazy <[email protected]> wrote: > Hi everyone. I'm not sure if this question belongs here but here > goes.. > > I am a professional asp.net developer looking to get into android dev. > > I have been going through tutorials and having fun so far! > > I have a question related to how the tutorial is asking me to > structure my code. > The code is from a tutorial where I create a gridview and set up a > click listener event: > gridview.setOnItemClickListener(new OnItemClickListener() { > public void onItemClick(AdapterView<?> parent, View v, > int position, long id) { > Toast.makeText(HelloGridViewActivity.this, "Position: > " + > position, Toast.LENGTH_SHORT).show(); > } > }); > > To me this structure is kind of messy, what I would like to do is have > the function, public void onItemClick be by itself, so that I can do > something more along the lines of: (psudo code) > gridview.setOnItemClickListener([somehow reference the function > here]); > > Is what I'm asking possible? > Does my question make sense? > Is there a more appropriate group to ask this kind of question? > > Thanks very much for your time. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

