【发布时间】:2011-08-09 08:15:21
【问题描述】:
我有一个相当奇怪的问题。我正在尝试将多个参数传递给不同类中的方法,并且四个参数中的三个传递得很好。然而,第四个是返回 0(零),无论我做什么。
我正在初始化第二个类 ImageLoader,没有任何问题。
这是有问题的电话 - 我添加了 cmets 来解释我的问题:
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);
}
【问题讨论】:
-
你为什么不告诉你如何获得变量“位置”......?
-
它是 BaseAdapter 中 GetView 方法中的位置变量。对不起我忘记了。但它完全有效,我在 GetView 方法中使用它没有问题。
-
你为什么不发布这个类的完整代码......
-
没有错! @Michell 请给我看你调用
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position);的代码块@ 它是一个函数吗?编辑您的问题 -
标签: java android methods parameters android-3.0-honeycomb