0

I'm using Cardslib library. I was trying to add thumbnail view for each pacakge from its icon. Icon is Drawable type. So I assume, we need to use CustomSource to create a Bitmap of it. Then add it to the card.

The problem with the below code is, all packages gets same thumbnail image. (The image from last package appearing on the list). Is this due to cardslib loads them using the built-in AsyncTask and LRUCache. How to resolve this issue?

public void listpkg(Context c) {
    ArrayList<Card> cards = new ArrayList<Card>();
    Card card = new Card(this);
    mContext = c;
    CardHeader header = new CardHeader(c);
    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    for (ApplicationInfo packageInfo : packages) {
        header = new CardHeader(this);
        header.setTitle(pm.getApplicationLabel(packageInfo).toString());
        card = new Card(this);
        card.addCardHeader(header);
        card.setTitle("Package: " + packageInfo.packageName);
        icon =getPackageManager().getApplicationIcon(packageInfo); //TODO use this icon
        tagname = packageInfo.packageName;
    // CustomSource --
             thumb = new CardThumbnail(c);
             thumb.setCustomSource(new CardThumbnail.CustomSource() {
                                          @Override
                                          public String getTag() {
                                              return tagname;
                                          }
                 @Override
                 public Bitmap getBitmap() {
                     PackageManager pm = mContext.getPackageManager();
                     Bitmap bitmap = null;
                     try {
                         bitmap = drawableToBitmap(pm.getApplicationIcon(getTag()));
                     } catch (PackageManager.NameNotFoundException e) {
                     }
                     return bitmap;
                 }

                 private Bitmap drawableToBitmap(Drawable drawable) {
                     if (drawable instanceof BitmapDrawable) {
                         return ((BitmapDrawable) drawable).getBitmap();
                     }

                     Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
                     Canvas canvas = new Canvas(bitmap);
                     drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                     drawable.draw(canvas);

                     return bitmap;
                 }
            });

            card.addCardThumbnail(thumb);
    // CustomSource --
            cards.add(card);
        }


    CardArrayRecyclerViewAdapter mCardArrayAdapter;

    mCardArrayAdapter = new CardArrayRecyclerViewAdapter(this, cards);

    //Staggered grid view
    CardRecyclerView mRecyclerView = (CardRecyclerView) this.findViewById(R.id.mainListView);
    mRecyclerView.setHasFixedSize(false);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    //Set the empty view
    if (mRecyclerView != null) {
        mRecyclerView.setAdapter(mCardArrayAdapter);
    }

}

xml

<it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:list_card_layout_resourceID="@layout/list_card_thumbnail_layout"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginTop="12dp"
    android:id="@+id/mainListView" />
webminal.org
  • 44,948
  • 37
  • 94
  • 125
  • is getTag() also returning same for all items ? – dumb_terminal Jun 05 '16 at 04:06
  • I think I have 50 apps or so when printed 'tagname' after the line "tagname = packageInfo.packageName;" they are different names. But Strangely, with-in getTag() : print appeared only twice with same name (google-contacts app name). Is it due the setCustomSource() runs as Asynctask ? and tagname is global variable? – webminal.org Jun 05 '16 at 07:42
  • 1
    check the answer. you are overwriting tagname with each iteration. – dumb_terminal Jun 05 '16 at 08:05

1 Answers1

1

Ok instead of keeping tagname to one variable and passing it to an inner implementation of CustomSource, you implement the CustomSource in another class and keep a field variable to hold the tagname. As in the current implementation the global (in this context) tagname is being replaced with each iteration.

class MyThumbnailSource implements CardThumbnail.CustomSource {
    private String tagname;

    public MyThumbnailSource(String tagname){
        this.tagname = tagname;
    }

    @Override
    public String getTag() {
        return tagname;
    }

    @Override
    public Bitmap getBitmap() {
        PackageManager pm = mContext.getPackageManager();
        Bitmap bitmap = null;
        try {
            bitmap = drawableToBitmap(pm.getApplicationIcon(getTag()));
        } catch (PackageManager.NameNotFoundException e) {
        }
        return bitmap;
    }

    private Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }
}

and call it like : thumb.setCustomSource(new MyThumbnailSource(tagname));

dumb_terminal
  • 1,815
  • 1
  • 16
  • 27