【问题标题】:image loading not working while passing HashMap to custom array adapter of Universal Image Loader将 HashMap 传递给 Universal Image Loader 的自定义数组适配器时,图像加载不起作用
【发布时间】:2015-06-15 18:50:35
【问题描述】:

我需要显示我的可绘制文件夹中的图像。我创建了一个整数数组来存储可绘制的 id,就像这样

private int[] itemImages = new int[]{
            R.drawable.image1, R.drawable.image2, R.drawable.image3,
            R.drawable.image4, R.drawable.image5, R.drawable.image6,
            R.drawable.image7, R.drawable.image8, R.drawable.image9,
            R.drawable.image10, R.drawable.image11, R.drawable.image12,
            R.drawable.image13, R.drawable.image14, R.drawable.image15,
            R.drawable.image16, R.drawable.image7, R.drawable.image18,
            R.drawable.image19, R.drawable.image20, R.drawable.image21,
            R.drawable.image22, R.drawable.image23, R.drawable.image24,
            R.drawable.image25, R.drawable.image26, R.drawable.image27
    };

还有一些其他文字itemPriceitemNames 然后创建一个 List 并将元素传递给自定义数组适配器

  List<HashMap<String, String>> imageArray = new ArrayList<HashMap<String, String>>();

           for (int i = 0; i < itemImages.length; i++) {
                HashMap<String, String> hm = new HashMap<String, String>();
                hm.put("name", "Name : " + itemNames[i]);
                hm.put("price", "Price : " + itemPrices[i]);
                hm.put("image", Integer.toString(itemImages[i]));
                imageArray.add(hm);
            }
    Custom_Adapter adapter = new Custom_Adapter(getApplicationContext(),
R.layout.imagelist_layout, imageArray);

自定义数组适配器中的getView是

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View displeyView = inflater.inflate(R.layout.imagelist_layout, parent, false);
    ImageView img = (ImageView) displeyView.findViewById(R.id.image);
    TextView name = (TextView) displeyView.findViewById(R.id.name);
    TextView price = (TextView) displeyView.findViewById(R.id.price);

    try {
        final HashMap<String, String> imgList = imageList.get(position);
        final String image = imgList.get("image");
        final String price1 = imgList.get("price");
        final String name1 = imgList.get("name");

        imageLoader.displayImage(image, img, options);
        name.setText(name1);
        price.setText(price1);
    } catch (Exception e) {
    }
    return displeyView;
}

通过这样做,文本正在显示。但我无法查看图像。显示 R.drawable.errorimage。 我该如何解决这个问题?

【问题讨论】:

    标签: android android-listview universal-image-loader


    【解决方案1】:

    如果您只是想将可绘制图像设置为 imageview 的背景,您可以使用

    imageView.setImageResource(yourDrawable);
    

    但我认为您有一些内存问题或需要使用 Unviersal Image Loader 的显示选项,例如下载圆角位图等。

    您可以使用通用图像加载器设置可绘制对象:

    String yourUrl = "drawable://" + R.drawable.your_drawable;
                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(yourUrl, yourImageView, displayOptions);
    

    希望对你有帮助。

    【讨论】:

    • 此代码正在运行,但图像以拥挤的格式显示
    • 我认为这取决于您的显示图像选项和您的图像视图布局属性。此代码仅向您展示如何使用通用图像加载器显示可绘制图像。把你的显示图像选项。顺便说一句,您的内存问题可能是由您的 List 引起的
    • 感谢您的帮助。我只需要在列表视图中安排图像视图。非常感谢:)
    • 很高兴为您提供帮助。如果我的回答完全解决了您的问题,请接受它作为“已接受的答案”:) 谢谢。
    【解决方案2】:

    通用图像加载器可帮助您以良好的效果在互联网上加载图像,并用于加载本地图像,更喜欢使用本地方式加载可绘制对象 — ImageView.setImageResource(...) 在我看来,而不是使用 ImageLoader 很好...

    使用

    img.setImageResource(image);
    

    而不是

    imageLoader.displayImage(image, img, options);
    

    为 uil

    首先改变你的数组

    private String[] itemImages = new String[]{
    
        "drawable://R.drawable.image1","drawable://R.drawable.image2", 
        ...
    };
    
    
    
       List<HashMap<String, String>> imageArray = new ArrayList<HashMap<String, String>>();
    
               for (int i = 0; i < itemImages.length; i++) {
                    HashMap<String, String> hm = new HashMap<String, String>();
                    hm.put("name", "Name : " + itemNames[i]);
                    hm.put("price", "Price : " + itemPrices[i]);
                    hm.put("image", (itemImages[i]));
                    imageArray.add(hm);
                }
        Custom_Adapter adapter = new Custom_Adapter(getApplicationContext(),
    
        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View displeyView = inflater.inflate(R.layout.imagelist_layout, parent, false);
        ImageView img = (ImageView) displeyView.findViewById(R.id.image);
        TextView name = (TextView) displeyView.findViewById(R.id.name);
        TextView price = (TextView) displeyView.findViewById(R.id.price);
    
        try {
            final HashMap<String, String> imgList = imageList.get(position);
            final String image = imgList.get("image");
            final String price1 = imgList.get("price");
            final String name1 = imgList.get("name");
    
            imageLoader.displayImage(image, img, options);
            name.setText(name1);
            price.setText(price1);
        } catch (Exception e) {
        }
        return displeyView;
    }
    

    【讨论】:

    • 发生内存不足异常
    【解决方案3】:

    试试这个代码:-

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View displeyView = inflater.inflate(R.layout.imagelist_layout, parent, false);
        ImageView img = (ImageView) displeyView.findViewById(R.id.image);
        TextView name = (TextView) displeyView.findViewById(R.id.name);
        TextView price = (TextView) displeyView.findViewById(R.id.price);
    
        try {
            final HashMap<String, String> imgList = imageList.get(position);
            final String image = imgList.get("image");
            final String price1 = imgList.get("price");
            final String name1 = imgList.get("name");
    
    
    Drawable drawable = getResources().getDrawable(getResources()
                        .getIdentifier(Integer.parseInt(image), "drawable", getPackageName()));
    
                img.setImageDrawable(drawable);  
    
    
            name.setText(name1);
            price.setText(price1);
        } catch (Exception e) {
        }
        return displeyView;
    }
    

    【讨论】:

    • 猫使用getResources.getDrawable
    猜你喜欢
    • 2015-10-06
    • 2020-03-24
    • 2012-11-19
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2013-07-15
    相关资源
    最近更新 更多