【问题标题】:Problem passing a parameter to a different class将参数传递给不同的类时出现问题
【发布时间】: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


【解决方案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)

【讨论】:

  • 08-09 10:27:42.820: VERBOSE/DisplayImage(29966): position = 0 这非常奇怪,我不知道为什么会这样。
  • 将其添加到 DisplayImage 的“顶部”显然会写入 0,因为“位置”尚未初始化。
  • 是的,我就是这么想的,所以我把它改成了:Log.v("DisplayImage", "position = " + pos);
  • @Sherif 是的,将其更改为 2。
  • 好的,我编辑了.. 很明显,位置始终为零,您对coverFileNames.get(position) 的看法是错误的@ 它不起作用
猜你喜欢
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 2015-09-10
  • 1970-01-01
  • 2011-08-03
  • 1970-01-01
相关资源
最近更新 更多