开发者

Sometimes my icons in my list view are not showing

开发者 https://www.devze.com 2023-04-09 00:10 出处:网络
My problem is that I have implemented a custom arrayadapter, that fetches images from an url and sets them to an imageview. It works, sort of. Sometimes some of the images are not showing up. Somtimes

My problem is that I have implemented a custom arrayadapter, that fetches images from an url and sets them to an imageview. It works, sort of. Sometimes some of the images are not showing up. Somtimes it's only one image, somtimes it's three and in no apparent order. Only thing that's reasonably consistent is that it seems to be image number 3 in my arraylist.

My custom adapter:

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 );
开发者_如何学Python    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;
}


having a blocking network part during your getView is not recommanded.

use

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

is certainly better.


I finally figured out the problem. The problem is that Android < 2.3 is not handling jpg images very well. There is one fix, which will fix this in most cases, but not all.

You can read more about it here: http://code.google.com/p/android/issues/detail?id=6066

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号