声明为全局变量即可。
ViewHolder holder = null;//放在函数外面
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.sound_list_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.soundId.setText(String.valueOf(position + 1));
holder.soundPath.setText(data.get(position).get("soundpath"));
//button点击事件
//播放
holder.playSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String, String> map = (HashMap<String, String>) lvSound.getItemAtPosition(position);
String soundpath = map.get("soundpath");
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(soundpath);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(BaseDataSheetActivity.this, "开始播放", Toast.LENGTH_SHORT).show();
holder.playSound.setImageDrawable(getResources().getDrawable(R.drawable.play_music_32));//可以使用了
} catch (IOException e) {
e.printStackTrace();
}
}
});
return convertView;
}