我需要在我的drawable文件夹中显示我的图像。我创建了一个整数数组来存储可绘制的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
};
以及其他一些文字itemPrice
和itemNames
然后创建一个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。 我该如何解决这个问题?
答案 0 :(得分:2)
如果您只想将可绘制图像设置为imageview的背景,则可以使用
进行设置imageView.setImageResource(yourDrawable);
但是我认为你有一些内存问题或需要使用Unviersal Image Loader的显示选项,例如下载为圆形位图等。
您可以使用通用图像加载器设置drawable:
String yourUrl = "drawable://" + R.drawable.your_drawable;
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(yourUrl, yourImageView, displayOptions);
我希望这对你有所帮助。
答案 1 :(得分:1)
通用图像加载程序可以帮助您在互联网上加载图像并获得良好的效果,并且可以加载本地图像,而不是使用本机方式来加载drawables - ImageView.setImageResource(...)而不是使用ImageLoader是很好的... / p>
使用
img.setImageResource(image);
而不是
imageLoader.displayImage(image, img, options);
for 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;
}
答案 2 :(得分:0)
尝试此代码: -
@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;
}