我的问题是我已经实现了一个自定义 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;
}