我有一个相当特殊的问题。我试图将一些参数传递给另一个类中的方法,并且四个参数中的三个被传递得很好。然而,第四个是返回0(零),不管我似乎做什么。
我正在初始化第二个类ImageLoader,没有任何问题。
这是有问题的电话 - 我已添加评论来解释我的问题:
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!
这是DisplayImage方法:
public int position;
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, final int pos) {
position = pos; // this is 0 no matter what I do
imageViews.put(imageView, fileUrl);
queuePhoto(fileUrl, activity, imageView);
imageView.setImageResource(R.drawable.noposterlarge);
}
有什么想法吗?感谢。
修改
这是GetView方法:
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;
}
同样,这是DisplayImage方法:
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);
}
答案 0 :(得分:1)
这根本不可能
添加到DisplayImage
Log.v("DisplayImage", "position = " + position);
如果是0
从
更改主叫代码imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position);
到
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, 2);
现在,它会显示2
吗? 必须
这意味着,在imageLoader.DisplayImage()
行中的调用代码由于某种原因始终为0
。
coverFileNames.get(position)
始终返回position=0
尝试从DisplayImage函数中删除final
修饰符
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, int pos)