Question

This might have been asked plenty of times already but I can't seem to get an actual answer to this. I have an adapter that takes data from an API and converts it into a listview. The row has its own Play button in it. When clicking on the play button, inside the row, it should change the button to a pause button. When I click on a different row's play button, the last button clicked should go back to a play button. I know it's setimageresource on the buttons but I can't seem to track my last button clicked. Here are my codes:

This is part of the adapter and

 @Override
            public View getView(final int pos, View ConvertView, ViewGroup parent) {

                final MyViewHolder holder;

            if (ConvertView == null){
                ConvertView = layoutinflater.inflate(R.layout.row, parent, false);
                holder = new MyViewHolder();        
                holder.plays = (ImageButton) ConvertView.findViewById(R.id.plays);
                holder.plays.setFocusable(false);
                play = playdata.get(pos);
                holder.posturl= play.getposturl();
                mStartPlaying = true;
                player = new MediaPlayer();
//              player.setOnCompletionListener(onCompletion);
//              player.setOnErrorListener(onError);

                holder.plays.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        String url = holder.posturl;


                        if (!mStartPlaying){
                                holder.plays.setImageResource(R.drawable.ic_action_stop);
                                startPlaying(url);
                                mStartPlaying = true;

                            } else{
                                holder.plays.setImageResource(R.drawable.ic_action_play);
                                player.stop();
                                mStartPlaying = false;
                            }

                    }

                    private void startPlaying(String url) {
                        // TODO Auto-generated method stub

                        try {

                            player.reset();
                            player.setDataSource(url);
                            // mPlayer.setDataSource(mFileName);
                            player.prepareAsync();
                            player.setOnPreparedListener(new OnPreparedListener() {

                                @Override
                                public void onPrepared(MediaPlayer mp) {

                                    player.start();


                                        }
                                      });


                        } catch (IOException e) {
                            Log.e("preparefailed", "prepare() failed");
                        }

                    }
                });

                if (!player.isPlaying()){
                    holder.plays.setImageResource(R.drawable.ic_action_play);
                }
                ConvertView.setTag(holder);
                ConvertView.setOnClickListener(new OnClickListener() {
                    public void onClick(View v){
                        Log.d("hi", "hi from "+ pos);
                    }
                });
            }
            else{
                holder = (MyViewHolder)ConvertView.getTag();
            }



            play = playdata.get(pos);
            holder.play = play;

            return ConvertView;



        }

This is my main activity:

mLastClickedPosition = -1;
this.PlayList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {



        if(mLastClickedPosition != -1){
              // do something to pause the item in your list at this position
        }

        // next, update mLastClickedPosition
        mLastClickedPosition = position;


        if(position==0){
              ImageView imageView = (ImageView) view.findViewById(R.id.plays);
              imageView.setImageResource(R.drawable.ic_action_stop);
        }

        // play audio


    }

});

the mediaplayer is initialized in the adapter, I'm not really sure how I'd go about initializing it in the main activity instead. Any help is appreciated! Thanks in advanced!

Was it helpful?

Solution

If each row in the ListView know what state it is in(butten), You could control each row in the ListView using listeners or LocalBroadcastManager. Try this. That is a ListView skeleton to start thinking in OOP where each row really becomes an object that could know what state it is in . A ListView abstraction example that makes it easier to understand/see the java code as objects.

From the example above, let say that every row(EventItem) that are created are a listener or register for LocalBroadcastManager Then it would be easy to address each row. Talk to each row and ask it to stop having the button in a pause state, or whatever

...I'm updating this answer since the link above doesn't work. Here's a new link to a gethub project

listview-with-multiple-layouts

OTHER TIPS

You should follow what Erik suggested and delegate the handling to each row.

BTW, could the problem be because of two listeners you have? You are maintaining the state in main activity which may not even be called. If this is not happening, you may need to move the maintaining state to the list listener (saving the last position).

But, follow what Erik suggested and that should help you to easy debugging and maintenance.

Good Luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top