Problem passing a parameter to a different class
I've got a rather peculiar problem. I am trying to pass a number of parameters to a method in a different class, and three out of the four parameters are being passed just fine. The fourth, however, is returning 0 (zero) regardless of what I seem to do.
I am initializing the second class, ImageLoader, without any issues.
Here's the call in question - I've added comments to explain my problem:
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position); // coverFileNames.get(position) works great and returns the correct filename based on the position - position on its own, however, doesn't!
And here's the DisplayImage method:
public int position;
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, final int pos) {
position = pos; // this is 0 no matter 开发者_Python百科what I do
imageViews.put(imageView, fileUrl);
queuePhoto(fileUrl, activity, imageView);
imageView.setImageResource(R.drawable.noposterlarge);
}
Any ideas? Thanks.
EDIT:
Here's the GetView method:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (ImageView) new ImageView(Main.this);
}
// Create new file for the file path of the movie
File file = new File(videoUrls.get(position));
// Create variables for potential custom art check
boolean potentialImage = false;
String ImageFile = null;
String[] potentialImageFiles = new String[]{file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".jpg",
file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".jpeg",
file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".JPG",
file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".JPEG"};
// Check if each file exists and return if one does
for (String potentialFile : potentialImageFiles) {
if (!potentialImage) {
if (new File(potentialFile).exists()) {
potentialImage = true;
ImageFile = potentialFile;
}
}
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inPreferredConfig = Config.RGB_565;
// Check if there's a custom cover art
if (potentialImage) {
Log.d("TEST", "POS: " + position); // this returns the correct value
imageLoader.DisplayImage(ImageFile, Main.this, (ImageView) convertView, position);
} else {
Log.d("TEST", "POS: " + position); // this returns the correct value
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position);
}
return convertView;
}
And again, here's the DisplayImage method:
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, final int pos) {
Log.v("Testing", "position = " + pos); // This returns 0, which is not correct
imageViews.put(imageView, fileUrl);
queuePhoto(fileUrl, activity, imageView);
imageView.setImageResource(R.drawable.noposterlarge);
}
this is simply impossible
Add to top of DisplayImage
Log.v("DisplayImage", "position = " + position);
If it is 0
Change the calling code from
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position);
to
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, 2);
Now, will it show 2
? It must
It means that in your calling code in the line imageLoader.DisplayImage()
position has always been 0
for some reason.
coverFileNames.get(position)
is always returning the String at position=0
Try removing the final
modifier from your DisplayImage function
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, int pos)
精彩评论