Reputation: 9295
I am using a ListView with images, also I have an image loader. The problem is that when there is a fast scrolling the images appears on wrong rows, after short time when the scrolling stops every thing gets back in place.
Here is my adapter:
public class MyAdapter extends ArrayAdapter<MyObj>
{
int layoutResourceId;
List<MyObj> data;
Activity activity;
int rowViewId;
public MyAdapter(Activity activity, int layoutResourceId, List<MyObj> data)
{
super(activity, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.activity = activity;
this.data = data;
rowViewId = R.layout.my_layout.xml;
}
public static class ViewHolder
{
public TextView title;
public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
ViewHolder holder;
if (row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(rowViewId, null);
holder = new ViewHolder();
holder.title = (TextView) row.findViewById(R.id.title);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
final MyObj obj = data.get(position);
if (event != null)
{
holder.title.setText(event.getTitle());
ImageLoader imageLoader = new ImageLoader();
imageLoader.loadImageFromAppServer(obj.getImageName(), holder.image);
}
return row;
}
}
Is there any thing I can do to prevent this unwanted behaviour?
Upvotes: 0
Views: 152
Reputation: 6605
Your image is being loaded asynchronously by loadImageFromAppServer
. When it arrives from the server, your holder.image
object is already reused by another row that doesn't represent the same data it did when you asked your ImageLoader
object to download the image. You need to implement a way to tell the ImageLoader
object to cancel the previous download request.
Upvotes: 1