ListView + ArrayAdapter: Setting OnClickListener to items 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?
Well it's not about performance in this case, but you could use dependency injection, which means you could pass the instance of the listener outside to the Adapter. Then just say

listItem.setOnClickListener(onClickListner);
This could make your code more readable 

UPDATE

Example shows, how to pass an OnClickListener outside from the class. 

So, I would like to add more code, to make sure we can learn from it together:

public class Malacka{

  private OnClickListener callbackListener;

  public Malacka(OnClickListener callback){
     this.callbackListener = callback; 
  }


  public void getView(View view, int position, View parent){
    // apply viewHolder pattern here and do your stuff
   itemView.setOnClickListener(callbackListener);
  }
}