I've recently written a Java program that displays a list of Word objects using a ListView and a custom ArrayAdapter.
I also want to set an OnClickListener on the items that plays an audio file (specified in the Word class).
This is the code I've written for it:
WordAdapter.java
:
/* Set OnClickListener on item */
// Create MediaPlayer for the audio file
final MediaPlayer audio = MediaPlayer.create(getContext(), currentWord.getAudioResourceId());
// Set OnClickListener
listItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!audio.isPlaying()) {
audio.start();
}
}
});
Is there a more efficient way to accomplish this behaviour?