Nisha
Nisha

Reputation: 45

When scroll in listview updated data changed to previous data

In a list view i have image, text and image contains default image and on the image i have a download button. when list load with download complete , every thing works fine but When i scroll the list-view the image just changed to static image.

Upvotes: 1

Views: 215

Answers (2)

faranjit
faranjit

Reputation: 1627

When you scroll list your getView method of adapter class is firing again. You should check each time if the picture that you want to show was downloaded in getView method. If it was not downloaded hold your static image but if you downloaded successfully set that image.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
...

    if ("downloaded") {
        imageView.setImage("your image");
    } else {
        imageView.setImageResource(R.drawable.static_image);
    }
}

Upvotes: 0

Kushan
Kushan

Reputation: 5984

This happens because in a listview, the views get recycled. As soon as you scroll, your view which goes out of display can get recycled and is reused in another row, thus leading to jumbling of images etc...

You will have to use a ViewHolder pattern in your adapter to handle this. Add all your views to your viewholder object and use them to set the images,text etc... this will stop the effect that you want.

see: https://dzone.com/articles/optimizing-your-listview for a tutorial

also see: https://developer.android.com/training/improving-layouts/smooth-scrolling.html for details from google.

NOTE: If you are downloading images from internet, you will either need a caching mechanism or use Picasso,Fresco... or such libraries to handle the slow convergence to the correct image.

The reason why i am suggesting a library is as follows:

1) when you scroll, the image will again go to network to get downloaded, this meanwhile your static placeholder will show up. It will take while for your image to get downloaded. Also unless you implement your own custom cache mechanism, it will always go to download the image.

2) when you scroll, the thread you spawn for downloading will keep running even if the view is out of display.

3) the libraries i mentioned, handle caching on their own... they cancel requests when your view goes off screen.... they handle garbage collection on their own....

Upvotes: 1

Related Questions