2

I have a list with a custom adapter and a custom row.

The problem is that when I scroll the listview, the images are at the wrong positions...

Here is the custom row xml:

<LinearLayout
    android:id="@+id/imagequality_listview_row_linearlayout"
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:weightSum="1"
    android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">


    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imagequality_listview_row_textview"
android:layout_width="0dp"
android:layout_height="40dip"
android:gravity="right"
android:paddingLeft="5dip"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
android:layout_weight="0.6"
/>

       <com.example.listviewtest.LoaderImageView
   android:id="@+id/imagequality_listview_row_loaderImageView"
   android:layout_width="0dp"
   android:layout_weight="0.4"
   android:layout_height="100dp"
   image="http://developer.android.com/images/dialog_buttons.png"
   />

</LinearLayout>

Here is the GetView:

List<ImageQuality_Item> items;
    Context context;
    List<CheckedTextView> checkedList;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.imagequality_listview_row, null);
            holder = new ViewHolder();
            holder.layout = (LinearLayout) convertView.findViewById(R.id.imagequality_listview_row_linearlayout);

            holder.textview = (CheckedTextView) convertView.findViewById(R.id.imagequality_listview_row_textview);
            holder.image = (LoaderImageView) convertView.findViewById(R.id.imagequality_listview_row_loaderImageView);
            checkedList.add(holder.textview);
            holder.layout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < checkedList.size(); i++)
                        if (checkedList.get(i).isChecked())
                            checkedList.get(i).setChecked(false);
                    ((CheckedTextView) ((LinearLayout) v).getChildAt(0)).setChecked(true);
                }
            });

            holder.textview.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < checkedList.size(); i++)
                        if (checkedList.get(i).isChecked())
                            checkedList.get(i).setChecked(false);
                    ((CheckedTextView) v).setChecked(true);
                }
            });
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.textview.setText(items.get(position).getDescription());
        if (holder.image.used == false) {
            holder.image.setImageDrawable(items.get(position).getURL());
            holder.image.used = true;
        }

        return convertView;
    }

    class ViewHolder {
        LinearLayout layout;
        LoaderImageView image;
        CheckedTextView textview;
    }

How can I fix this?

Omar
  • 7,835
  • 14
  • 62
  • 108

1 Answers1

3

You have to set the Drawable every time getView() gets called, since android is reusing the list elements. It is likely that an earlier set image appears, if you dont update it.

Just remove your condition to set the image drawable, and it will work.

Adam Monos
  • 4,287
  • 1
  • 23
  • 25
  • But now after scrolling down and then returning to the previous shown items, the images are again reloaded.. Thats why I put the condition which prevented from this happening but also made the bug. How can I fix this? – Omar Aug 02 '12 at 21:11
  • You can't. As i said, android is reusing the inflated list item layouts, to save memory. If you dont initialize the items every time, your data will be inconsistent. – Adam Monos Aug 03 '12 at 06:56