Search code examples
androidandroid-arrayadapterandroid-mediaplayer

Android Issue with values of array inside custom adapter


I have a ListView that I'm populating through a Custom Adapter. Everything in that adapter works ok, I evaluate if I'm getting a video, an image or an audio file and then take the correct action.

The thing is that with audio files it is not working like it should. I log the audio file name and it is correct but when handling the onclick event, no matter what file I click on, it always plays the same one. And in the logs in the onclick event it shows the same all the time, I can not figure it why.

Appreciate any help

Code of adapter:

notaV = resultp.get(MainActivity.AUDIO); //this gives me http://www.xx.com/audio/file.amr
Log.e("audio adapter", notaV); //works good
Log.e("quien nota",resultp.get(MainActivity.QUIEN_ID));
reporte.append(" "+notaV); // I show the name of the file in a textview

//Then I load an drawable inside an imageview and set the listener
//to that image, when clicked look for play the audio
if (notaV.length() != 0) {
    imgs.setImageResource(R.drawable.playnote);

    imgs.setVisibility(View.VISIBLE);

    imgs.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent();  
            intent.setAction(android.content.Intent.ACTION_VIEW);
            //now here is when it always shows the same file over and over. 
            //I've tried with no final String or just using notaV
            //but with same result.
            final String audio = notaV;
            Log.e("voz",audio);
            //also the ID of the user that sent the audio it's being logged
            // as myself, while in previous Log it showed the correct ID.
            Log.e("quien click",resultp.get(MainActivity.QUIEN_ID));
            intent.setDataAndType(Uri.parse(audio), "audio/*");  
            context.startActivity(intent);
        }
    });
}

Solution

  • try to do something like this

      if (notaV.length() != 0) {
      imgs.setImageResource(R.drawable.playnote);
    
      imgs.setVisibility(View.VISIBLE);
      imgs.setTag(notaV);    
      imgs.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View view) {
    
            String audioUrl =  view.getTag.toString();
            Intent intent = new Intent();  
            intent.setAction(android.content.Intent.ACTION_VIEW);
            //now here is when it always shows the same file over and over. 
            //I've tried with no final String or just using notaV
            //but with same result.
            final String audio = audioUrl;
            Log.e("voz",audio);
            //also the ID of the user that sent the audio it's being logged
            // as myself, while in previous Log it showed the correct ID.
            Log.e("quien click",resultp.get(MainActivity.QUIEN_ID));
            intent.setDataAndType(Uri.parse(audio), "audio/*");  
            context.startActivity(intent);
        }
      });
    }