2012-07-04 49 views
0

我有這種循環方法來顯示ViewPager,我爲ViewPager上的每個圖像設置一個OnClickListener。所以點擊圖片時,用戶將被帶到VideoPlayer類。但我不知道爲什麼,點擊偵聽器的footage_video_url[i]putExtra()總是讀取額外的i是最大長度。所以i總是超過最大陣列長度,並使應用程序崩潰。一個循環OnClickListener總是得到錯誤的索引數組

我期待的是,如果例如顯示的圖像是footage_thumbnail_url[1],則額外放置的字符串分別爲footage_video_url[1],依此類推。 有人知道我出錯的地方嗎?

for(i = 0; i < length; i++){ 
       final String[] video_url = footage_video_url; 
       final String[] thumbnail_url = footage_thumbnail_url; 

       //System.out.println("FOOTAGE VIDEO DETAILS " +i+ ": " +video_url[i]); 

       FrameLayout content = new FrameLayout(this); 
       FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
         (int) size, (int) size); 
       params.gravity = Gravity.CENTER; 

       ImageView imageView = new ImageView(this); 
       ImageLoader loader = new ImageLoader(this); 

       // Image from URL 
       if (footage_thumbnail_url[i].contains("http")) 
        loader.downloadPicture(imageView, footage_thumbnail_url,i); 
       else { 
        // Image From SDCard 
        File imgFile = new File(thumbnail_url[i]); 
        Bitmap bitmapImage = loader.decodeFile(imgFile); 
        Drawable drawableImage = new BitmapDrawable(bitmapImage); 
        imageView.setBackgroundDrawable(drawableImage); 

        imageView.setOnClickListener(new View.OnClickListener() { 
         public void onClick(View v) { 
          Intent listIntent = new Intent(getApplicationContext(), 
            VideoPlayer.class); 
          listIntent.putExtra("video_url", footage_video_url[i]); 
          System.out.println("Video Number "+i+" : "+ footage_video_url[i]); 
          startActivity(listIntent); 
         } 
        }); 
       } 


       content.addView(imageView); 

       View overlay = new View(this); 
       overlay.setBackgroundResource(R.drawable.btn_play); 
       params.gravity = Gravity.CENTER; 
       overlay.setLayoutParams(params); 
       overlay.invalidate(); 

       content.addView(overlay); 
       pagerViews.add(content); 



      } 

回答

1

嘗試設置爲ImageView字符串footage_video_url[i]的標記,然後在OnClickListener中檢索它。這樣你就可以確定正確的字符串將被放入臨時演員:

imageView.setTag(footage_video_url[i]); 
imageView.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
      String theUrl = (String) v.getTag(); // I hope it's a String 
      Intent listIntent = new Intent(getApplicationContext(), 
            VideoPlayer.class); 
      listIntent.putExtra("video_url", theUrl); 
      System.out.println("Video Number "+i+" : "+ theUrl); 
      startActivity(listIntent); 
    } 
}); 
+0

令人驚歎!工作喜歡魅力,謝謝! – hectichavana

1

將imageView作爲長度爲n的數組。那麼你的for循環之前,添加1更多的循環,日子會把你設置的標籤爲所有的「N」 imageViews,這樣的事情:

for(int x=0;x<length;x++) 
    imageView[x].setTag(x); 

那麼你的循環內更換這些線路:

listIntent.putExtra("video_url", footage_video_url[i]); 
System.out.println("Video Number "+i+" : "+ footage_video_url[i]); 

這些行:

listIntent.putExtra("video_url", footage_video_url[imageView[i].getTag(i)]); 
System.out.println("Video Number "+i+" : "+ footage_video_url[imageView[i].getTag(i)]); 

希望這可以幫助!

相關問題