coreORB / SelectionDialogs

Selection Dialogs is Android library allowing quickly create colors and icons selection dialogs, and providing simple views to display selected items.
Apache License 2.0
55 stars 11 forks source link

setIcons method not work #2

Open DigitalChip opened 7 years ago

DigitalChip commented 7 years ago

Wonderful library! But... I can't set icons for IconChooseDialog with this method:

...
   .setIcons(R.array.icons_ids, R.array.icons_names,R.array.icons_drawables)
...

If i use sampleIcons() method (as in example), then all is ok.

...
   .setIcons(sampleIcons()) 
...

My arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons_ids">
        <item>icon1</item>
        <item>icon2</item>
        <item>icon3</item>
    </array>

    <array name="icons_names">
        <item>Icon 1</item>
        <item>Icon 2</item>
        <item>Icon 3</item>
    </array>

    <integer-array name="icons_drawables">
        <item>@drawable/ic_menu_send</item>
        <item>@drawable/icon2</item>
        <item>@drawable/icon3</item>
    </integer-array>
</resources>

DigitalChip commented 7 years ago

scr2 scr1

DigitalChip commented 7 years ago

Solution: Need use obtainTypedArray for get TypedArray in method convertResourceArraysToIconsArrayList(...):

public static ArrayList<SelectableIcon> convertResourceArraysToIconsArrayList(Context context, boolean sortByName, @ArrayRes int idsArray, @ArrayRes int namesArray, @ArrayRes int drawablesArray) {
   //get and check arrays
        String[] ids = context.getResources().getStringArray(idsArray);
        String[] names = context.getResources().getStringArray(namesArray);
        TypedArray drawables = context.getResources().obtainTypedArray(drawablesArray);

        if (ids.length != drawables.length() && ids.length != names.length) {
            Log.e("ICONS_SELECT", "convertResourceArraysToIconsArrayList(): Arrays must have equals lengths!");
            return null;
        }

        //create ArrayList
        ArrayList<SelectableIcon> result = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            result.add(new SelectableIcon(ids[i], names[i], drawables.getResourceId(i,-1)));
        }

        //sort by names
        if (sortByName) {
            Collections.sort(result, new SelectableItemNameComparator<SelectableIcon>());
        }

        return result;
}