3

For some reason, my first image displays correctly, then gets overwrriten with another user's image. Any ideas:

public View getView(int position, View convertView, ViewGroup parent) { 
        View vi=convertView; 
        ViewHolder holder;
        if( convertView == null ){
                vi = inflater.inflate(R.layout.feed_item, null);
                holder=new ViewHolder();
                holder.userImage = (ImageView) vi.findViewById(R.id.feed_userimage);
                vi.setTag(holder);
        } else {
                holder=(ViewHolder)vi.getTag();
        }
        if(user.has("image") && 
          user.getString("image") != null && 
          !user.getString("image").equals("null")) {
                holder.userImage.setTag(user.getString("image"));
                imageLoader.DisplayImage(user.getString("image"), act, holder.userImage,USER_IMAGE_SIZE);
        } else {
                holder.userImage.setImageDrawable(null);
        }
hunterp
  • 15,716
  • 18
  • 63
  • 115

2 Answers2

1

Try this fix

if(user.has("image") && 
    user.getString("image") != null && 
    !user.getString("image").equals("null")) {
  holder.userImage.setTag(user.getString("image"));
  imageLoader.DisplayImage(user.getString("image"), act, holder.userImage,USER_IMAGE_SIZE);
} else {
   holder.userImage.setTag(null);//add this line
   holder.userImage.setImageDrawable(null);
}
Fedor
  • 43,261
  • 10
  • 79
  • 89
  • Sorry, the correct image still shows up for a second, and then as the rest of the list loads, it is replaced with another user's image. – hunterp Sep 26 '11 at 21:14
  • Does it work the same when you use my sample application? If not can you identify the difference between your code and my sample? – Fedor Sep 26 '11 at 23:53
  • I mean does it work the same when you use my sample application from here https://github.com/thest1/LazyList? – Fedor Sep 27 '11 at 05:06
  • Could you please give me a sample project with this problem? I need a way to reproduce the problem to debug it. – Fedor Oct 02 '11 at 01:51
  • I have installed the latest lazylist from github and I'm liking the new memory management....well done! It might have also solved the issue. The jury is still out, but things are looking good! – hunterp Oct 03 '11 at 19:28
  • But the jury has returned....bleh. I will have to isolate this problem sooner rather than later. – hunterp Oct 04 '11 at 03:49
  • When I clear the cache before the list loads, everything works fine, however, if I don't then this problem occurs. I cannot give source, but I'd be happy to isolate some sample code. Any ideas? – hunterp Oct 13 '11 at 08:19
  • I would be glad to take a look at the sample if you could isolate it. – Fedor Oct 13 '11 at 11:01
  • I just spent 2 hours isolating the code into a sample project. The list rendered images correctly....until I added the ImageList into a TabHost. Tabhost is definitely introducing errors. ALSO,I put your LazyList demo into a tab, and it works fine (after restarting 15x), but this does not change the fact that my code still renders the first image incorrectly about 10% of the time only when I add tabs. – hunterp Oct 14 '11 at 17:26
  • Even wierder.....I read the same JSON from a file, and there is no problem. And I read it from the net, there is a delay and the first image is incorrect. And indeed, I remove the tabs, and load the json from the net and everything works fine. WTF?? FYI, I'm setting the adapter in onPostExecute – hunterp Oct 14 '11 at 18:09
1

It is happenning because you are using convertView. The convertView passed to the getView method is essentially a view object you had created (by inflating) in a previous getView call (which is no more be needed because it is not visible anymore, due to scrolling).

You are assuming that every time getView is called a new view is created, whereas you are actually using previously created views. The convertView is passed as an optimisation so that too many views does not have to be created when only a few are visible. So the setTag call actually overwrites a previously created view's tag.

You should reconsider rewriting your code without using tags. Or, you could always inflate the view, instead of only inflating when the convertView is not null. But I wouldn't reccommend this approach as for a long list it would mean too many unnecessary views in the memory.

Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69
  • The reason you have given is correct, the culprit is Android caching the views and the fact that the getview(0) is called more frequently. But the fix is still not apparent. Still looking for it. – Nitin Jan 06 '12 at 07:54