ᴛʜᴇᴘᴀᴛᴇʟ
ᴛʜᴇᴘᴀᴛᴇʟ

Reputation: 4656

Passing image into Android ListView ArrayAdapter

I have a ListView and I am passing value to this list using ArrayAdapter. This is what I did.

first I created string array:

private static readonly string[] menuItemNames = new string[] {
    "Home", "New", "Delete", "Submit", "Help"
};

And then created ArrayAdapter:

this.MyMenuList.Adapter = new ArrayAdapter<string> (this, Resource.Layout.item_menu, Resource.Id.MenuItemText, menuItemNames);

And this seems to work fine. The values from menuItemNames array goes into the list and makes a new item. But next to each item, I have an icon. How Can I set the icon of each one?

This is what I tried:

Drawable sideIcon1 = context.Resources.GetDrawable (Resource.Drawable.home_icon);
Drawable sideIcon2 = context.Resources.GetDrawable (Resource.Drawable.new_icon);
Drawable sideIcon3 = context.Resources.GetDrawable (Resource.Drawable.delete_icon);
Drawable sideIcon4 = context.Resources.GetDrawable (Resource.Drawable.submit_icon);
Drawable sideIcon5 = context.Resources.GetDrawable (Resource.Drawable.help_icon);

Then, made drawable array:

Drawable[] sideMenuItemIconsDrawables = new Drawable[] {
    sideIcon1, sideIcon2, sideIcon3, sideIcon4, sideIcon5
};

And then make a new ArrayAdapter:

this.MyMenuList.Adapter = new ArrayAdapter<Drawable> (this, Resource.Layout.item_menu, Resource.Id.ImgView, sideMenuItemIconsDrawables);

This doesn't seem to work. What do I nee to pass instead of drawable to change the image of that ImageView ?

Thank you for your time.

// ===========================================================

EDIT: So this is what I tried. But it crashes my app before even the view loads. The adapter count changes but it just crashes after that.

        IList<IDictionary<string, object>> menuItemNames = new List<IDictionary<string, object>> ();

        Dictionary<string, object> sideItem1 = new Dictionary<string, object> ();
        sideItem1.Add ("text", "SideMenuItem1");
        sideItem1.Add ("icon", Resource.Drawable.menu_Icon);

        Dictionary<string, object> sideItem2 = new Dictionary<string, object> ();
        sideItem2.Add ("text", "SideMenuItem2");
        sideItem2.Add ("icon", Resource.Drawable.back_arrow_box);

        Dictionary<string, object> sideItem3 = new Dictionary<string, object> ();
        sideItem3.Add ("text", "SideMenuItem3");
        sideItem3.Add ("icon", Resource.Drawable.bar_addAbsence);

        Dictionary<string, object> sideItem4 = new Dictionary<string, object> ();
        sideItem4.Add ("text", "SideMenuItem4");
        sideItem4.Add ("icon", Resource.Drawable.bar_addExpense);

        Dictionary<string, object> sideItem5 = new Dictionary<string, object> ();
        sideItem5.Add ("text", "SideMenuItem5");
        sideItem5.Add ("icon", Resource.Drawable.bar_addTime);

        menuItemNames.Add (sideItem1);
        menuItemNames.Add (sideItem2);
        menuItemNames.Add (sideItem3);
        menuItemNames.Add (sideItem4);
        menuItemNames.Add (sideItem5);

        string[] sideMenuFromArr = { "text", "icon" };

        int[] sideMenuToArr = { Resource.Id.textSideMenuItem, Resource.Id.imgViewSideMenuItemIcon };

        SimpleAdapter sideMenuListAdapter = new SimpleAdapter (context, menuItemNames, Resource.Layout.item_menu, sideMenuFromArr, sideMenuToArr);
        this.absHomeSideMenuList.Adapter = sideMenuListAdapter;

Upvotes: 2

Views: 1031

Answers (1)

born_genius
born_genius

Reputation: 36

There are several ways of solving that, to me I think using SimpleAdapter is the simplest way. Make a List of HashMap of string and object. Create your layout, populate your List with the text and icons you wish to use and pass the layout and List to your SimpleAdapter constructor. Something like:

    List<HashMap<String, Object>> menuItemNames = new ArrayList<HashMap<String,Object>>();
    HashMap<String, Object> row = new HashMap<String, Object>();
    row.put("icon", R.drawable.home_icon);
    row.put("text", "Home");
    menuItemNames.add(row);
    row = new HashMap<String, Object>();
    row.put("icon", android.R.drawable.new_icon);
    row.put("text", "New");
    menuItemNames.add(row);
    row = new HashMap<String, Object>();
    row.put("icon", android.R.drawable.delete_icon);
    row.put("text", "Delete");
    menuItemNames.add(row);

    int[] to = {R.id.sideMenuItemIconsDrawables, R.id.MenuItemText}; //IDs of your text and icon
    String[] from = {"icon", "text"};

    adapter = new SimpleAdapter(this, menuItemNames, R.layout.item_menu, from, to);
    listView.setAdapter(adapter);

I hope this helps.

Upvotes: 2

Related Questions