0

gridviev に非同期で画像をロードしています。しかし、私の gridview は gridview の最後のセルに 1 つの画像しか表示していません。私のアダプター クラスと非同期クラスを以下に示します。

アダプタ クラス:

class OrderAdapter extends ArrayAdapter<String>
{   
    LayoutInflater inflater;

    String name3[];
    public OrderAdapter(Context context,int resource,LayoutInflater inflater,String name2[])
    {
        super(context, resource,R.id.img,name2);
        this.inflater=inflater;
        this.name3=name2;
    }
    public View getView(int position, View convertView, ViewGroup parent)
    {  
        View row=inflater.inflate(R.layout.row,parent,false);
      final  ImageView img=(ImageView)row.findViewById(R.id.img);
        String imgurl=name3[position];
        Log.e("urlchandan",name3[position]);
        AsyncImageLoaderv asyncImageLoaderv=new AsyncImageLoaderv();
        Bitmap cachedImage = asyncImageLoaderv.loadDrawable(imgurl, new AsyncImageLoaderv.ImageCallback() 
            {
            public void imageLoaded(Bitmap imageDrawable, String imageUrl) {
            img.setImageBitmap(imageDrawable);

            }
            });
        img.setImageBitmap(cachedImage);     

        return row;
    }

}

非同期クラス

public class AsyncImageLoaderv {
    private HashMap<String, SoftReference<Bitmap>> imageCache;

    public AsyncImageLoaderv() {
        imageCache = new HashMap<String, SoftReference<Bitmap>>();
    }

    public Bitmap loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
        if (imageCache.containsKey(imageUrl)) {
            SoftReference<Bitmap> softReference = imageCache.get(imageUrl);
            Bitmap drawable = softReference.get();
            if (drawable != null) {
                return drawable;
            }
        }
        final Handler handler = new Handler() {
            @Override
                public void handleMessage(Message message) {
                    imageCallback.imageLoaded((Bitmap) message.obj, imageUrl);
                }
        };
        new Thread() {
            @Override
                public void run() {
                    try{
                        Log.d("ur",imageUrl);
                        Bitmap drawable = loadImageFromUrl(imageUrl);
                        imageCache.put(imageUrl, new SoftReference<Bitmap>(drawable));
                        Message message = handler.obtainMessage(0, drawable);
                        handler.sendMessage(message);
                    }catch(Exception e){Log.e("thread stellent",e.toString());}
                }
        }.start();
        return null;
    }


    public static Bitmap loadImageFromUrl(String url) {
            InputStream inputStream;Bitmap b;
            try {
                    inputStream = (InputStream) new URL(url).getContent();
                    BitmapFactory.Options bpo=  new BitmapFactory.Options();
                    bpo.inSampleSize=2;
                   b=BitmapFactory.decodeStream(new PatchInputStream(inputStream), null,bpo );
                    return  b;
            } catch (IOException e) {
                    throw new RuntimeException(e);
                }
//return null;
    }

    public interface ImageCallback {
        public void imageLoaded(Bitmap imageBitmap, String imageUrl);
    }
}
4

2 に答える 2

1

あなたがしようとしている方法でそれを行うことはできません。非同期ローダーに結果の画像を、アダプターが位置でアクセスできるデータ構造 (リスト、ハッシュマップなど) に格納する必要があります。次に、正しい位置から画像を引っ張るgetView()だけです。非同期ローダーはデータ構造にデータを入力し、 a を実行しnotifyDataSetChanged()て、新しく読み込まれた画像でリスト自体を再描画します。

于 2011-05-12T15:03:50.387 に答える
0

画像が gridview の単一のセルに表示されるのを避けるため、adatper inflater の ImageView img を final にすることで解決策を得ました。そして、私の画像はサイズが大きく、エラーデコーダーがfalseを返し、このエラーは別のクラスを取ることで解決されました

--

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class PatchInputStream extends FilterInputStream {
      public PatchInputStream(InputStream in) {
        super(in);
      }
      public long skip(long n) throws IOException {
        long m = 0L;
        while (m < n) {
          long _m = in.skip(n-m);
          if (_m == 0L) break;
          m += _m;
        }
        return m;
      }
    }

このクラスは、上記の AsyncImageLoaderv で使用されます。

  b=BitmapFactory.decodeStream(new PatchInputStream(inputStream), null,bpo );
于 2011-06-15T14:31:19.540 に答える