将HashMap传递到Universal Image Loader的自定义数组适配器时,图像加载不起作用

问题描述 投票:1回答:3

我需要显示可绘制文件夹中的图像。我创建了一个整数数组来存储可绘制的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<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
3个回答
2
投票

[如果您只是尝试将可绘制的图像设置为imageview的背景,则可以使用]进行设置>

imageView.setImageResource(yourDrawable);

但是我认为您有一些内存问题,或者需要使用Unviersal Image Loader的Display Options,例如四舍五入的位图下载。

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

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

我希望这会对您有所帮助。


1
投票

[通用图像加载器可帮助您以良好的效果在Internet上加载图像,并以本地方法加载本地图像,而不是使用本机方法来加载可绘制对象– ImageView.setImageResource(...)代替使用ImageLoader很好,根据我的看法...


0
投票

尝试此代码:-

© www.soinside.com 2019 - 2024. All rights reserved.