0
\$\begingroup\$

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?

asked Jun 13, 2017 at 18:48
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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);
 }
}
answered Jun 21, 2017 at 18:10
\$\endgroup\$
0
2
\$\begingroup\$

I'm not totally sure because you only shared small section of your code but it seems that you are creating a new click listener for every list item you have and it's a performance issue. creating new objects means more memory usage and slower code, in case of list views user may feel lacks on weaker devices. The better way to do it is to only define one click listener and assign all your list items to that. If you want to know witch item is clicked you can use tags on views. myView.setTag(whatever) and then get the tag by myView.setTag()

Also you can switch to RecyclerView. Since Android 5.0 RecyclerView can do anything a ListView or GridView can do and more with optimized performance, It also force View Recycling witch has a great effect on performance as well.

answered Sep 1, 2017 at 9:00
\$\endgroup\$
2
  • \$\begingroup\$ If I used listView.setOnItemClickListener(), would I be more efficient? \$\endgroup\$ Commented Sep 1, 2017 at 18:24
  • \$\begingroup\$ Yes it will be better since you only define your listener once. see my updated answer about recyclerview as well. \$\endgroup\$ Commented Sep 2, 2017 at 11:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.