Android: Array index when i first click the button it doesn't show my first index but it show the second index

时间:2015-06-15 15:20:57

标签: android

I'm creating an app on android for viewing the image with the sound.I got problem when i first click the next button it doesn't show the my first index image with sound but it show my second index. i want to show my first index when i first click on the next button.

   public void btn_click(View v){
  ImageView imageview = (ImageView)findViewById(R.id.ImageViewPreview3);
  if (v == btn_next) {
          try {
              currentimageindex = (currentimageindex + 1) % IMAGE_IDS.length;
              currentsoundindex = (currentsoundindex + 1) % myMusic.length;
          } catch (Exception e) {
              Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
          }
      }
  if (v == btn_back) {
          try {
              currentimageindex = (currentimageindex + IMAGE_IDS.length - 1) % IMAGE_IDS.length;
              currentsoundindex = (currentsoundindex + myMusic.length - 1) % myMusic.length;
          } catch (Exception e) {
              Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
          }
      }
  imageview.setImageResource(IMAGE_IDS[currentimageindex]);
  mp = MediaPlayer.create(this, myMusic[currentsoundindex]);
  mp.start();
  }
}

1 个答案:

答案 0 :(得分:0)

使用equals(Object)代替==

if (v.equals(btn_next)) {
          try {
              currentimageindex = (currentimageindex + 1) % IMAGE_IDS.length;
              currentsoundindex = (currentsoundindex + 1) % myMusic.length;
          } catch (Exception e) {
              Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
          }
      }

等等。