Loading image from URL in custom adapter for ListView (Android Studio)

帅比萌擦擦* 提交于 2019-12-01 00:07:19

The easiest way to solve your problem is using some library, for example Picasso. It is not only easier to use and make your code easier to read, but also prevents you from OutOfMemory Exceptions, when images are too big. Loading image is then matter of one line:

Picasso.with(context)
.load(urlOfYourImage)
.resize(50, 50) // here you resize your image to whatever width and height you like
.into(imageView)

I think you're running into two issues here:

1) View Recycling - I'm new to Android myself, and this comes up pretty frequently when using listviews, when parts of the listview move off screen, they can be wrong when scrolling back. I personally had an issue with button states changing for the wrong rows when scrolling up and down in one. This tutorial goes over what Recycling is in a bit more detail, and provides a solution in a View Holder private class that worked for me. I'm sure other parts of SO talk about this too.

2) Image Null - Is this definitely coming through as null, or just not appearing in the listview? If the former, some step in LoadImage() must not be working correctly. If the latter, you may want to try calling notifyDataSetChanged() after setting the image, to tell the main thread the image is ready to be drawn. Something like below (in your AsyncTask):

@Override
protected void onPostExecute() {
       notifyDataSetChanged();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!