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

前端 未结 2 1091
粉色の甜心
粉色の甜心 2021-01-06 09:48

While the bitmap seems to be fetched right, the variable \'userBitmap\' will remain null. When scrolling up or down on my tablet, though, new list rows will contain the pic

2条回答
  •  日久生厌
    2021-01-06 10:38

    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();
        }
    }
    

提交回复
热议问题