public class SomeClass {
public void SomeMethod() {
GridView gv = new GridView(this);
gv.setOnClickListener(new GridView.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
// Define onClick here and reference it somehow above?
}
How can I take the onClick method out of that block of code into the root of the class? Like you can do it on C#, for instance.
You can declare a Listener as any other class attribute:
Or implement the OnClickListener (or other) interfaces, but you will need to know which which view was clicked (switch inside the OnClick handler) if several views will report to this listener. I tend not to use this approach – it looks less readable for me.