2

i am using the ListView with a custom object inflated into the getView method.

My issue starts when the listview triggers the onItemClicked event, it repaints the items.

That is not the issue. The actual issue appears because I am recycling the items that getView gives me.

Before I get into that, Here is the Code I am using.

public View getView(int position, com.abc.rxcard.Item itemAtPosition,
        View convertView, ViewGroup parent) {
    LinearLayout thisItem;
    //Get the current alert object

    //Inflate the view
    if(convertView==null)
    {
        thisItem = new LinearLayout(this);
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi;
        vi = (LayoutInflater)this.getSystemService(inflater);
        vi.inflate(R.layout.offerrow, thisItem, true);
    }
    else
    {
        thisItem = (LinearLayout) convertView;
    }

    Log.d("thisItem","This Item is Index " + position + " " + thisItem.toString() + " " + Integer.toHexString(System.identityHashCode(thisItem)));


    //Get the text boxes from the listitem.xml file
    TextView caption =(TextView)thisItem.findViewById(R.id.caption);
    TextView subText =(TextView)thisItem.findViewById(R.id.subText);
    ImageView icon = (ImageView)thisItem.findViewById(R.id.offerIcon);

    //Assign the appropriate data from our alert object above
    caption.setText(itemAtPosition.text());
    subText.setText(itemAtPosition.subText());
    MyUtils.setImageFromURLInBackgroundThread(icon, itemAtPosition.icon());


    return thisItem;
}

and the issue arrises when I click on the items. the log output is as follows

04-20 14:36:28.402: DEBUG/thisItem(12495): This Item is Index 0 android.widget.LinearLayout@44db1c50 44db1c50
04-20 14:36:28.432: DEBUG/thisItem(12495): This Item is Index 1 android.widget.LinearLayout@44da9038 44da9038
04-20 14:36:28.442: DEBUG/thisItem(12495): This Item is Index 2 android.widget.LinearLayout@44d2ee48 44d2ee48
04-20 14:36:28.932: DEBUG/thisItem(12495): This Item is Index 0 android.widget.LinearLayout@44d2ee48 44d2ee48
04-20 14:36:28.952: DEBUG/thisItem(12495): This Item is Index 1 android.widget.LinearLayout@44da9038 44da9038
04-20 14:36:28.962: DEBUG/thisItem(12495): This Item is Index 2 android.widget.LinearLayout@44db1c50 44db1c50
04-20 14:36:29.622: DEBUG/thisItem(12495): This Item is Index 0 android.widget.LinearLayout@44db1c50 44db1c50
04-20 14:36:29.652: DEBUG/thisItem(12495): This Item is Index 1 android.widget.LinearLayout@44da9038 44da9038
04-20 14:36:29.662: DEBUG/thisItem(12495): This Item is Index 2 android.widget.LinearLayout@44d2ee48 44d2ee48

as you can see the Indexes 0 and 2 are flipping the items back and forth. Now this would not be an issue if the items were not actually on the screen. However when I click on the items (and because the images are loaded in a background thread) My images on items 1 and 3 flip back and forth for every click.

This seems like a bad behavior.
I am not sure what in the world to do, to fix the problem. Is there a setting or something that I can put on the ListView to stop it from doing that ?

2 Answers 2

3

It turns out that the recycler is going through all my items and putting them in the scrap heap because it will be refreshing them this run cycle. The problem I am having is because it goes through the items in reverse order and I should take for granted that I need to erase the object and then set its values again.

My resolution to this problem is to cache the images I am downloading to disk in a specified place for at least a minute. and when the refresh happens it will attempt to load the image from cache rather then downloading it each time. and will only download when the image is expired. Thereby replacing the image Immediately with the image it needs rather then holding onto the existing scrap image.

The the problem I was facing is resolved but the fact still remains that they recycle the items that are on the screen.... its hard to believe that on an item click the entire ListView will be refreshed and all the items will be re-created. but that is the way the cookie crumbles, when you dip it in java....

0

I had the same problem before.

I think the issue is with your background image thread downloader. If you scroll the list view really fast, and because the same row view is being reused over and over again, potentially you'll have all these images trying to download to the same "icon" image view, and setting images one on-top of another, which creates the effect of images switching back and forth.

My solution is to set tag with an unique identifier, on the icon image view before calling the image downloader. for example say:

icon.setTag(imageUrl);

MyUtils.setImageFromURLInBackgroundThread(icon, itemAtPosition.icon(), imageUrl);

And inside your setImageFromURLInBackgroundThread() method, when you finish downloading your image on the thread, check the icon view to see if the download url matches the imageUrl before setting to the icon image view. Example:

if (icon.getTag() != null) {
    viewTagUrl = String.valueOf(icon.getTag());
}

if(imageUrl.equalsIgnoreCase(viewTagUrl)) {
    icon.setImageBitmap(bitmap);
}

Also, because image download sometime takes a while, I also clean up the image view and set a default image before running the thread to download the image.

icon.setImageBitmap(null);
icon.setImageResource(defaultImage);
...
downloadThread.start();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.