1

タイトル、サブタイトル、および画像を ListView に追加する Lazy アダプターがあります。最近、ListView で画像が複製されていることに気付きました。

ここで、アレイをアダプターに追加します

protected void onPostExecute(String result) {                   
    LazyAdapter adapter = new LazyAdapter(Services.this,menuItems);     
    serviceList.setAdapter(adapter);
}

ここで、非同期メソッドを呼び出して画像を追加します(非同期メソッドにあるのはURLです)

vi.findViewById(R.id.thumbnail).setVisibility(View.VISIBLE);
title.setText(items.get("title")); // Set Title
listData.setText(items.get("htmlcontent")); // Set content          
thumb_image.setBackgroundResource(R.layout.serviceborder);//Adds border
new setLogoImage(thumb_image).execute(); // Async Method call

そして、ここでビットマップ画像を設定します

private class setLogoImage extends AsyncTask<Object, Void, Bitmap> {
        private ImageView imv;            

        public setLogoImage(ImageView imv) {
             this.imv = imv;                 
        }

    @Override
    protected Bitmap doInBackground(Object... params) {
        Bitmap bitmap = null;
        try {               
            bitmap = BitmapFactory.decodeStream((InputStream)new URL(items.get("thumburl")).getContent());

        } catch (MalformedURLException e) {             
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }
    @Override
    protected void onPostExecute(Bitmap result) {           
        imv.setImageBitmap(Bitmap.createScaledBitmap(result, 50, 50, false));
    }
}

items変数は、私の情報を格納する HashMap です。アダプターに与えられたデータの位置を取得し、アイテム HashMap に割り当てます。そのようです:

items = new HashMap<String, String>();
items = data.get(position);

これはランダムに発生するようで、約 30% の確率で、特にデバッグしようとしているときに、間違った画像が表示されるのが面倒です。

どんな助けでも素晴らしいでしょう。ありがとう

ここに何が起こっているかを見るための画像があります ここに画像の説明を入力

4

1 に答える 1

1

ランダムに発生する何かがあり、何らかの種類のスレッドがある場合はいつでも、Race Condition という 2 つの機能を考える必要があります。基本的に、AsyncTasks はすべて同じイメージをロードしています。これらは、ロードする値で渡す必要があります。paramsで何もしていないことに気付きました。何をすべきか正確にはわかりませんが、問題は AsyncTask のどこかにあります。

@Override
protected Bitmap doInBackground(Object... params) {
    Bitmap bitmap = null;
    try {               
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(items.get("thumburl")).getContent());

    } catch (MalformedURLException e) {             
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

おそらく URL (またはそれが表す文字列) を渡す必要があります。この例では URL を渡していますが、必要に応じて自由に変更してください。重要なことは、関数内で使用する画像を見つけようとするのではなく、画像の場所の表現を関数に渡す必要があるということです。

new setLogoImage(new Url(items.get("thumburl"))).execute(); // Async Method call


@Override
protected Bitmap doInBackground(Url... input) {
    Bitmap bitmap = null;
    try {               
        bitmap = BitmapFactory.decodeStream((InputStream)input[0]).getContent());

    } catch (MalformedURLException e) {             
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}
于 2013-01-31T21:32:46.603 に答える