有时我的列表视图中的图标没有显示

时间:2011-09-29 07:38:42

标签: android android-arrayadapter android-imageview custom-adapter

我的问题是我已经实现了一个自定义arrayadapter,它从一个url获取图像并将它们设置为一个imageview。它有效。有时一些图像没有出现。有时它只有一个图像,有时它是三个,并且没有明显的顺序。唯一合理一致的是它似乎是我的arraylist中的第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;
}

2 个答案:

答案 0 :(得分:0)

在getView期间不建议使用阻塞网络部分。

使用

setImageUri(Uri.parse(o.getIcon());

肯定更好。

答案 1 :(得分:0)

我终于找到了问题所在。问题是Android< 2.3不能很好地处理jpg图像。有一个修复,在大多数情况下会解决这个问题,但不是全部。

您可以在此处详细了解:http://code.google.com/p/android/issues/detail?id=6066

相关问题