I have this loop method to display a ViewPager and I set an OnClickListener for each Image on the ViewPager. So when the image is clicked, the user will be brought to a VideoPlayer class. but I don’t know why, the footage_video_url[i] on putExtra() of the click listener always reads that the i on the extra is the maximum length. So the i always exceeds the maximum array length and that makes the app crash.
What I was expecting is, if for example the displayed image is footage_thumbnail_url[1], the String which is put on extra is footage_video_url[1] respectively, and so on.
Does anybody know where I go wrong?
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);
}
Try to set as the tag for the
ImageViewthe stringfootage_video_url[i]and then retrieve it in theOnClickListener. This way you’re sure the correct String will be put in the extras: