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

标签 android android-listview universal-image-loader

我需要显示可绘制文件夹中的图像。我像这样创建了一个整数数组来存储可绘制对象的 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.erroimage。 我该如何解决这个问题?

最佳答案

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

进行设置
imageView.setImageResource(yourDrawable);

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

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

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

希望对您有所帮助。

关于android - 将 HashMap 传递给 Universal Image Loader 的自定义数组适配器时图像加载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29554868/

相关文章:

android - 操作项的自定义下拉菜单 (actionbarsherlock)

android - Smack/openfire 如何让用户永久留在群聊室

Android ListView 内存泄漏?

android padding - 有时在 4.2.2 中被忽略,在 4.0.4 中有效

java - ImageLoader-java.lang.OutOfMemoryError

android - 有没有办法在 TabHost 中显示 fragment ?

android - 覆盖 View ID

android - Android ListView 中的 HTML 文本

java - 有没有办法在使用通用图像加载器获取图像时指定额外的 header ?

android - 使用 Universal Image Loader 在 GridView 中加载图像