Android 有时我的列表视图中的图标不显示
我的问题是,我实现了一个定制的arrayadapter,它从url获取图像并将其设置为imageview。在某种程度上,它是有效的。有时一些图像没有显示出来。有时它只是一个图像,有时它是三个,而且没有明显的顺序。唯一合理一致的是,它似乎是我的arraylist中的3号图像 我的自定义适配器:Android 有时我的列表视图中的图标不显示,android,android-arrayadapter,android-imageview,custom-adapter,Android,Android Arrayadapter,Android Imageview,Custom Adapter,我的问题是,我实现了一个定制的arrayadapter,它从url获取图像并将其设置为imageview。在某种程度上,它是有效的。有时一些图像没有显示出来。有时它只是一个图像,有时它是三个,而且没有明显的顺序。唯一合理一致的是,它似乎是我的arraylist中的3号图像 我的自定义适配器: public CustomAdapter( Context context, int textViewResourceId, List items ) { super( context, t
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;
}
不建议在getView期间使用阻塞网络部件 使用
当然更好。我终于解决了这个问题。问题是Android<2.3不能很好地处理jpg图像。有一种方法可以解决大多数情况下的问题,但不是全部 您可以在这里了解更多信息:My getIcon()返回一个字符串,这与SetImageUri不太兼容,这就是我编写“Uri.parse(o.getIcon())”的原因
setImageUri(Uri.parse(o.getIcon());