0

Hello i have a fully working code for my list adapter:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.overview_item, null);

    //getting id's
    TextView name =(TextView)vi.findViewById(R.id.userUsername);
    TextView date =(TextView)vi.findViewById(R.id.imageDate);

    ImageView image=(ImageView)vi.findViewById(R.id.userImage);
    ImageView avatar=(ImageView)vi.findViewById(R.id.userAvatar);

    //setting text
    name.setText(dataNames.get(position));
    date.setText(dataDates.get(position));
    //set image
    Log.d("test: ", "Adapter wants to get picture");
    imageLoader.DisplayImage(dataImage.get(position), image);
    imageLoader.DisplayImage(dataAvatars.get(position), avatar);
    return vi;
}

This code works perfect but the problem is this function runs everytime when you scroll throught the listview so whenever the lis item is getting in sight. And that's not what i want. i want it to do this function just once for every list item. This is because when your scrolling fast trought the list it has to load all images again so the loading image is showing and it keeps jumping because the loading image is another size then the image wich is getting loaded. I hope thay tou understand my question and can help me. Already thanks and if i'm not clear please ask my anything in the comments.

So short: How do i run this code just once for every list-item and not everytime when it's getting in sight?

Menno van Hout
  • 39
  • 1
  • 3
  • 8
  • this is the listview mechanism! u should cancel the imageLoader or check in ImageLoader , if row is in sight = show it, other = ignore – Saeed-rz Oct 29 '15 at 19:42
  • you have to understand first "How ListView's recycling mechanism works". you can't do this way what you want. First understand recycling mechanism from http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works and then solve your problem easyly – Amir Oct 29 '15 at 19:48

2 Answers2

1

Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.

Check this links:

1 - http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html

Andrew Dmytrenko
  • 380
  • 3
  • 11
  • Im not figuring out how i have to make a viewholder im struggeling with: HashMap item = (HashMap) getItem(position); holder.img.setTag(item.get("img")); How does he know what img is? – Menno van Hout Oct 29 '15 at 21:39
  • Okay wait i made the viewHolder but the problem is he only loads the first 2 pictures and places them everywhere? – Menno van Hout Oct 29 '15 at 21:44
0

No, you should not. This is the way ListView works. Beside, you should use ViewHolder pattern for better performance. If you still want to do this, you could remove check NULL with convertView. It will solve your problem, but lead to performance, I think.

duynt
  • 128
  • 8