0

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();
        }
    }
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • You should handle the click and subsequent changes in the adapter's `getView()` method. – Vikram Jul 25 '13 at 06:59
  • @vikram can you give me an example in an answer? – Darko Petkovski Jul 25 '13 at 07:05
  • where do you get your position and oldposition variables from - probably there is something wrong with them. Btw: The listView.getChildAt(position) method counts the number of visible children. Not the number as used in the adapter. So as vikrma suggest you should probably update your backing model and then tell the ListAdapter that the Data changed - its an easier way. – Simon Meyer Jul 25 '13 at 07:20
  • @SimonMeyer yes but how can i do that? – Darko Petkovski Jul 25 '13 at 07:34

2 Answers2

1

simply save the active position whereever you use your listadapter - then add some switch to your getView() Method like :

if (position==activePosition)
{
    playSong.setBackgroundResource(R.drawable.play_nr);
}
else
{
    playSong.setBackgroundResource(R.drawable.stop);
}

with your

public void changePicToPlay(int position) {
   activePosition = position;
   adapter.notifyDataSetChanged();
}

you don't have to set the mediaplayers listeners so often then too - in onCompletion you could simply call:

activePostion=-1;
adapter.notifyDataSetChanged();
Simon Meyer
  • 1,946
  • 12
  • 23
1

here are the same solution about your question. There is changing backgroundColor, you can replace it with setBackground();

Community
  • 1
  • 1
KEYSAN
  • 885
  • 8
  • 23