我遇到了一个相当奇怪的问题。我试图将多个参数传递给不同类中的方法,并且四个参数中的三个传递得很好。然而,无论我做什么,第四个都会返回 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);
}
最佳答案
这根本不可能
添加到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)
关于java - 将参数传递给不同的类时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6993222/