I have a listview with three icons per every row. If I press an icon(play) the icon needs to change the background to another(stop). And then if I click the same icon in a different row, the icon that is changed before needs to switch to the previous background (stop to play), and the newly clicked icon needs to change the background to second state(play to stop).
Here is the code i'm using but seems it is not working properly. It changes the clicked one but if I click the same icon in a new row it doesn't changes the icon in the previously clicked row.
From the adapter:
playSong.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying() && mediaPlayer != null) {
mediaPlayer.release();
if (oldPosition != -1) {
((AlbumDetails) activity).changePicToPlay(oldPosition,
playSong);
}
mediaPlayer = new MediaPlayer();
} else {
oldPosition = position;
try {
mediaPlayer.setDataSource(data.get(position)
.getSONG_MP3());
final ProgressDialog bufferingDialog = new ProgressDialog(
activity);
bufferingDialog.setMessage(activity
.getString(R.string.buffering));
bufferingDialog.show();
mediaPlayer.prepareAsync();
mediaPlayer
.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
bufferingDialog.dismiss();
mediaPlayer.start();
}
});
postData(1, position);
mediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
playSong.setBackgroundResource(R.drawable.play_nr);
mediaPlayer.release();
}
});
((AlbumDetails) activity).changePicToStop(oldPosition);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
From my activity:
public void changePicToStop(int position) {
View vi = listView.getChildAt(position);
if (vi != null) {
ImageView playSong = (ImageView) vi
.findViewById(R.id.songs_item_play);
playSong.setBackgroundResource(R.drawable.stop);
}
}
public void changePicToPlay(int position, ImageView image) {
View vi = listView.getChildAt(position);
if (vi != null) {
image.setBackgroundResource(R.drawable.play_nr);
listView.refreshDrawableState();
}
}