0

i have a big problem with my ListViewAdapter.

My listview shows 2 entrys at the same time. Each entry should get a different picture from my server. The first and the second entrys working fine, but if i'm scrolling down, the next entrys will have the same pictures.

My Code looks like this:

if (viewHolder.imgPic != null) {
    String strUrl = mainUrl+list.get(position).getUrl();
    new ImageDownload(viewHolder.imgPic).execute(strUrl);
}

I'm checking the view and just doing it, if it's null.

Can someone help me? Thanks

user3384194
  • 141
  • 1
  • 1
  • 11

2 Answers2

1

from your question I can assume that you don't know about the ListView recycling mechanisem

basically, view that that not visible anymore (after user scrolled it away from sight) it been recycled to displayed new item that need to shown. that's the convertView parameter at the getView() method...

probably you are see the same image because the recycled view stays with the same image..

also there is the issue of the asynchronous task (your ImageDownload class) that can finish it execute when the original item that started the request already been recycled.

I recommend you to "dig" as dipper as you can to understand all about ListView - this is one of the most complex and important UI component. reading the post I linked you is a good start.

also this video is very important:

http://www.youtube.com/watch?v=wDBM6wVEO70

Community
  • 1
  • 1
Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
0

Here is my GetView:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if(rowResourceId!=R.layout.search_empty_list) {
            if (convertView == null) {
                LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflator.inflate(rowResourceId, null);
                final ViewHolder viewHolder = new ViewHolder();

                viewHolder.textName = (TextView) view.findViewById(R.id.textView1);
                viewHolder.imgPic = (ImageView) view.findViewById(R.id.imgPic);         

                if (viewHolder.imgPic != null) {
                    String strUrl = mainUrl+list.get(position).getUrl();
                    new ImageDownload(viewHolder.imgPic).execute(strUrl);
                }

                view.setTag(viewHolder);

            } else {
                view = convertView;
            }
            ViewHolder holder = (ViewHolder) view.getTag();

            holder.textName.setText(list.get(position).getName());


        } else {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(rowResourceId, parent, false);

            TextView textView1 = (TextView) view.findViewById(R.id.textView1);
            textView1.setText(list.get(0).getName());
        }
        return view;

    }