【问题标题】:LazyAdapter duplicates imagesLazyAdapter 复制图像
【发布时间】:2013-01-31 21:27:17
【问题描述】:

我有一个惰性适配器,可以将标题、副标题和图片添加到 ListView。最近我在我的 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% 的时间在那里看到错误的图片会很烦人,尤其是在我尝试调试时。

任何帮助都会很棒。谢谢

这是一张图片,看看发生了什么

【问题讨论】:

标签: android multithreading listview android-asynctask android-lazyadapter


【解决方案1】:

任何时候你有随机发生的事情,并且你有任何类型的线程,你应该考虑两个工作,Race Condition。基本上,您的 AsyncTasks 都在加载相同的图像。它们应该传入要加载的值。我注意到你没有对参数做任何事情。我不确定您应该做什么,但问题出在您的 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;
}

【讨论】:

  • 你可以传递字符串,但关键是传递你想要使用的东西,不要试图在AsyncTask内部获取它。
  • 好吧,使用您的代码,我得到“无法从 URL 转换为 InputStream”。有什么想法吗?
  • 或者,您也可以只传递items.get("thumburl"),然后进行与最初相同的转换。
  • 是的,这就是我最终所做的。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多