Fayaz Bangash
Fayaz Bangash

Reputation: 61

Update Play, Pause button in listview

I have a listview, which have image, title, options and play/pause button. When i click on a play button in a row and then click on the play button in another row, the background of both buttons remain pause. So i want that when i click on play button in a row and then click on the play button in another row, the background of first button should change to play and background of second should remain pause. Thanks in advance

enter image description here

Here is the code

public class MyAdapter extends BaseAdapter{

    int itemnum; 
    Boolean playing = false;
    Context context; 
    int layoutResourceId;    

    // RingTone data[] = null;
    private LayoutInflater mInflater;

    String[] Title;
    int[] Img;

    ListView listview;






    public MyAdapter(Context context, int layoutResourceId, String[] Title,int[] Img, int[] Tone, ListView listview) {
        mInflater = LayoutInflater.from(context);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.Title=Title;
        this.Img=Img;
        ListActivity.Tone=Tone;
        this.listview=listview;



    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RingToneHolder holder = null;


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listrow, null);


        }


        holder = new RingToneHolder();
        // RingTone aa = data[position];
        holder.imgIcon = (ImageView)convertView.findViewById(R.id.list_image);
        holder.txtTitle = (TextView)convertView.findViewById(R.id.title);
        holder.playbtn = (Button)convertView.findViewById(R.id.playbtn);
        holder.btn_alert_dialog = (Button)convertView.findViewById(R.id.alert_dialog_btn);
        holder.pausebtn = (Button)convertView.findViewById(R.id.pausebtn);


        holder.txtTitle.setText(Title[position]);
        // holder.txtTitle.setText(aa.title);
        holder.imgIcon.setImageResource(Img[position]);
        //holder.playbtn.setInputType(aa.btn);S
        holder.playbtn.setTag(position);
        holder.btn_alert_dialog.setTag(position);



        holder.playbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // TODO Auto-generated method stub
                if(mp == null){

                    listview.invalidateViews();
                    mp = MediaPlayer.create(context,Tone[Integer.parseInt(arg0.getTag().toString())]);

                    mp.start();
                    playing = true;
                    itemnum = Integer.parseInt(arg0.getTag().toString());
                    //Toast.makeText(context, "ist playing", Toast.LENGTH_LONG).show();
                    arg0.setBackgroundResource(R.drawable.pause);

                }
                else {
                    if(mp.isPlaying() && itemnum == Integer.parseInt(arg0.getTag().toString())){
                        listview.invalidateViews();
                        mp.pause();
                        playing = false;
                        //Toast.makeText(context, " paused", Toast.LENGTH_LONG).show();
                        arg0.setBackgroundResource(R.drawable.play_btn);
                    }
                    else{
                        if(playing == false  && itemnum == Integer.parseInt(arg0.getTag().toString())){
                            listview.invalidateViews();
                            mp.start();
                            playing = true;
                            arg0.setBackgroundResource(R.drawable.pause);
                        }
                        else {
                            //.invalidate();
                            listview.invalidateViews();
                            mp.stop();
                            mp.release();
                            mp = MediaPlayer.create(context,Tone[Integer.parseInt(arg0.getTag().toString())]);
                            mp.start();
                            playing = true;
                            itemnum = Integer.parseInt(arg0.getTag().toString());
                            //Toast.makeText(context, "playing", Toast.LENGTH_LONG).show();
                            arg0.setBackgroundResource(R.drawable.pause);
                        }
                    }
                }

            }


        });

Upvotes: 4

Views: 956

Answers (1)

shiladitya
shiladitya

Reputation: 2310

You are only changing the drawable of the row being clicked.

Keep a pointer/index to the row which is currently playing, so that next time onClick() is fired, you know which row was playing previously. So go and change the drawable of that row too.

Upvotes: 1

Related Questions