title: ListView play button
body: <p>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:</p>
  
  <p>This is part of the adapter and </p>
  
  <pre><code> @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;
  
  
  
          }
  </code></pre>
  
  <p>This is my main activity:</p>
  
  <pre><code>mLastClickedPosition = -1;
  this.PlayList.setOnItemClickListener(new OnItemClickListener() {
  
      @Override
      public void onItemClick (AdapterView&lt;?&gt; 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
  
  
      }
  
  });
  </code></pre>
  
  <p>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!</p>