0

I am trying to create a custom adapter for a shop element in my Android app but every time I scroll down the list the images seem to change their position, I have had a look around on here already but suggestions that I have found and tried have been to no avail.

Each item has a title, an image (being loading via an AsyncTask or retrieved from the LRUCache) and a price of the item below is the code I use to generate the adapter.

public class ShopAdapter extends BaseAdapter {
    public Context context;
    public ArrayList<ShopItem> shopItemList;
    public ImageCache imageCache;
    String imageToLoad;
    Bitmap shopImage;

    public ShopAdapter(Context context, ArrayList<ShopItem> shopItemList) {
        this.context = context;
        this.shopItemList = shopItemList;
        imageCache = new ImageCache(this.context);
    }
    @Override
    public int getCount() {
        return shopItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return shopItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.shop_list, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.textView3);
            holder.image =(ImageView) convertView.findViewById(R.id.imageView);
            holder.price = (TextView) convertView.findViewById(R.id.textView4);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(shopItemList.get(position).getTitle());

        shopImage = imageCache.getBitmapFromMemoryCache(shopItemList.get(position).getImage());
        if(shopImage != null) {
            holder.image.setImageBitmap(shopImage);
        } else {
            new LoadImage(context, holder.image).execute(imageToLoad);
        }

        holder.price.setText(shopItemList.get(position).getPrice());
        return convertView;
    }

    static class ViewHolder {
        TextView title;
        ImageView image;
        TextView price;
    }
}

Any help would be very much appreciated!

  • Possible duplicate of [Why items change order upon scrolling in Android GridView?](http://stackoverflow.com/questions/10672273/why-items-change-order-upon-scrolling-in-android-gridview) – Onkar Nene Sep 09 '16 at 09:43
  • @OnkarNene Thank you! Will give that a go and see if that works :) – Chris Taylor Sep 09 '16 at 10:13

0 Answers0