How Do I Properly Use An Setonitemclicklistener?
Solution 1:
I wouldn't call myself a newcomer
Ummmm...
When I try to put my code inside the setOnItemClickListener but I get a warning saying that my Object, of the class I want to call the methods from, must be final to use in the inner class of another method.
That is because the objects in question are either parameters to setOnItemClickListener()
or are local variables, as opposed to being data members of the class where your play()
method resides.
Bear in mind that OnItemClickListener
is an interface, and so you do not need to use an anonymous inner class as you presently are. For example, you could implement this interface on the Activity
that (presumably) is hosting the play()
method.
Anonymous inner classes are a powerful, but expert, Java technique. I have taught many Android courses, and there are lots of experienced Java developers who do not recognize, let alone use, anonymous inner classes. Given your relative experience level, I would recommend that you avoid their use for the time being wherever possible.
Here is the part of my code that has the Listener
You are calling setOnItemClickListener()
in a loop. This is probably not what you want.
I need a way to use the Listener in other places of my code though.
No, you don't. You may need to have the listener do other things from its onItemClick()
method, but the listener itself, as an object, is only going to be used in this one place, barring some seriously strange coding.
So it would look something like this I would imagine - gv.onTouchEvent1.checkPiece(player1, position); -
Or implement checkPiece()
on the Activity
that is (presumably) hosting all of this code, in which case you just call it.
I have been learning Java for a year now, my first language
To be completely blunt, your problems have little to do with Android and everything to do with Java. Feel free to ask java syntax and related questions here on StackOverflow, but do so with the java tag (perhaps in addition to the android tag) if it is likely to be related more to Java and less regarding Android's classes and methods.
Post a Comment for "How Do I Properly Use An Setonitemclicklistener?"