Search code examples
androidsqlitebitmapblobcardslib

CardListView gets the same image in all its CardViews after iterating sqlite rows


I'm using cardslib library in my Android Studio project. I'm working on a CardListView. I'm retrieving each CardView information from an sqlite database row which has a BLOB field (used to store images). As shown in this picture, after filling CardListView from database, everything seems to be fine except for the CardThumbnail. It only shows the last iterated database row Bitmap field in every CardView of the list.

I'm using this code to iterate every sqlite row and fill CardListView items :

    /* Retrieving items from database */
    List<Item> items = dataBase.getAllItemsLOC();
    int itemsLOCCount = items.size();

    /* Iterating retrieved database items */
    for (int i = 0; i < itemsLOCCount; i++) {
        final Item item = items.get(i);

        RentCard card = new RentCard(getActivity());

        /* Add thumbnail */
        CardThumbnail cardThumbnail = new CardThumbnail(getActivity());
        cardThumbnail.setCustomSource(new CardThumbnail.CustomSource() {
            @Override
            public String getTag() {
                return "com.google.android.apps.maps";
            }

            @Override
            public Bitmap getBitmap() {
                Bitmap bitmap = item.getImageBitmap();
                return bitmap;
            }
        });
        card.addCardThumbnail(cardThumbnail);

        card.setDevise(item.getDevise());

        /* ... setting the rest of the CardView properties ... */

        card.setId("" + items.get(i).getId());

        cards.add(card);
    }
    mCardArrayAdapter.notifyDataSetChanged();

Could you please help me identify the problem?


Solution

  • Thanks to Rami, I solved my problem by replacing this part of the code :

    @Override
    public String getTag() {
        return "com.google.android.apps.maps";
    }
    

    By this :

    @Override
    public String getTag() {
        return item.getId();
    }
    

    Now, my CardListView is correctly displaying each CardThumbnail Image from each sqlite row as shown in this picture.