播放/停止按钮工作正常。但背景drawable无法正常工作

时间:2013-07-17 04:54:07

标签: android android-listview android-mediaplayer android-view

While playing second song我实现了播放/停止按钮。当我在播放时按下相同的按钮时,它应该是停止或暂停&&背景也改变了游戏 - >当我在播放时按下另一个按钮时,它应该停止并播放新歌。它可以工作。但是无法改变背景播放--->停。帮帮我。如何获得上一首歌的id值......

         public class MySimpleArrayAdapter extends ArrayAdapter<String>{
protected static final String TAG = "MySImpleArrayAdapter";
private final Context context;
protected ListView mListView;
int itemno;
 Boolean playing = false;
private MediaPlayer mp;
private LayoutInflater inflater;
 String[] values;  
 int songs []=                   {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5,R.raw.sound6,R.raw.sound7};
  static class ViewHolder {
        public TextView text;
        public Button button;

      }

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.activity_main, values);
    inflater = LayoutInflater.from(context);
    this.context = context;
    this.values = values;

  }


 @Override
  public View getView( int position, View rowView, ViewGroup parent) {

     if (rowView == null) {

          rowView = inflater.inflate(R.layout.activity_main, null);
          ViewHolder viewHolder = new ViewHolder();
          viewHolder.text = (TextView) rowView.findViewById(R.id.label);
                  viewHolder.button = (Button) rowView
              .findViewById(R.id.logo);

          rowView.setTag(viewHolder);
        }

        ViewHolder holder = (ViewHolder) rowView.getTag();
        String s = values[position];
      final int   pos=position;
        holder.text.setText(s);
        holder.button.setOnClickListener(
                new OnClickListener() {
                    public void onClick(View v) {
                         if(mp == null){

                              //  mListView.invalidateViews();
                                mp = MediaPlayer.create(context,songs[pos]);

                                mp.start();
                                playing = true;
                                itemno = pos;
                                //Toast.makeText(context, "ist playing", Toast.LENGTH_LONG).show();
                                v.setBackgroundResource(R.drawable.ok);

                            }
                            else {
                                if(mp.isPlaying() && itemno == pos){

                                    mp.pause();
                                    playing = false;

                                    v.setBackgroundResource(R.drawable.no);
                                }
                                else{
                                    if(playing == false  && itemno == pos){

                                        mp.start();
                                        playing = true;
                                        v.setBackgroundResource(R.drawable.ok);
                                    }
                                    else {

                                        mp.stop();

                                        mp.release();
                                        mp = MediaPlayer.create(context,songs[pos]);
                                        mp.start();
                                        playing = true;
                                        itemno = pos;
                                        //Toast.makeText(context, "playing", Toast.LENGTH_LONG).show();
                                        v.setBackgroundResource(R.drawable.ok);

                                    }if (!mp.isPlaying())
                                        v.setBackgroundResource(R.drawable.no);

                                }
                            }

                    }
                });






        return rowView;


      }

 @Override
 public boolean  areAllItemsEnabled() {
     return false;          
 }

 @Override
 public boolean isEnabled(int position) {
         return false;
 }

}

1 个答案:

答案 0 :(得分:1)