有时我的图标在我的列表视图中没有显示
问题描述:
我的问题是我已经实现了一个自定义的arrayadapter,它从url中获取图像并将它们设置为imageview。它的工作,有点。有时候某些图像没有显示出来。有时候这只是一张图片,有时是三张,没有明显的顺序。唯一合理一致的是,它似乎是我的数据列表中的图像编号3。有时我的图标在我的列表视图中没有显示
我的自定义适配器:
public CustomAdapter(Context context, int textViewResourceId, List items)
{
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, parent, false);
}
Object o = items.get(position);
if (o != null)
{
TextView textView = (TextView) v.findViewById(R.id.listItem);
ImageView imageView = (ImageView) v.findViewById(R.id.icon);
if (textView != null)
{
textView.setText(o.toString());
}
if (imageView != null)
{
if (o instanceof XmlGuide)
{
try
{
imageView.setImageBitmap(downloadIcon(((XmlGuide) o).getIcon()));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
return v;
}
private Bitmap downloadIcon(String uri) throws Exception
{
URL url = new URL(uri);
InputStream stream = url.openStream();
Bitmap icon = BitmapFactory.decodeStream(stream);
stream.close();
return icon;
}
答
我终于明白了这个问题。问题是,Android的< 2.3没有很好地处理JPG图像。有一个修复程序,它将在大多数情况下解决这个问题,但不是全部。
你可以阅读更多关于它在这里:http://code.google.com/p/android/issues/detail?id=6066
答
有你的getView期间阻塞网络部分不建议报告。
使用
setImageUri(Uri.parse(o.getIcon());
肯定是更好。
我调用getIcon()返回一个字符串,它并没有真正与setImageUri –
这就是为什么我写了“Uri.parse工作这么好(o.getIcon()) “ – njzk2