Android问题,包含自定义适配器内的数组值

时间:2014-06-04 05:08:03

标签: android android-arrayadapter android-mediaplayer

我有一个ListView我填写Custom Adapter。该适配器中的所有内容都可以正常工作,我评估我是否获得了视频,图像或音频文件,然后采取了正确的操作。

事实是,对于音频文件,它不能像它应该的那样工作。我记录音频文件名并且它是正确的但是当处理onclick事件时,无论我点击什么文件,它总是播放相同的文件。在onclick事件的日志中它始终显示相同,我无法理解为什么。

感谢任何帮助

适配器代码:

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);
        }
    });
}

1 个答案:

答案 0 :(得分:1)

尝试做这样的事情

  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);
    }
  });
}