java - 将参数传递给不同的类时出现问题

标签 java android methods parameters android-3.0-honeycomb

我遇到了一个相当奇怪的问题。我试图将多个参数传递给不同类中的方法,并且四个参数中的三个传递得很好。然而,无论我做什么,第四个都会返回 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/

相关文章:

android - 如何在 AOSP 中更改应用程序无响应 (ANR) 超时

android - 我如何在android中获取Phone实例?

随处可用的 Java 函数(方法)(全局)

java - 转义正则表达式字符串中的每个文字而不是引用整个字符串

java - 将选择的日期而不是当前日期获取到 JDBC

android - OpenCv C++ 裁剪图像

ruby-on-rails - Ruby on Rails : create action doesn't work, 而新建、编辑和更新(与创建中的形式相同!)操作有效 - 为什么?

java - Websphere 未将 request.sendRedirect 上的请求定向到代理服务器

Java继承ArrayList类型in方法

java - Java 中以对象作为参数的泛型方法