2

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;

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks a lot. I think this will work. I am using c# lang so I have to use Dictionary instead of HashMap. It is a little different but same idea. But i am not sure what String[] from is. Can you please tell me? Thanks
The last two arguments are string array and integer array, the string array holds the keys of the objects in the HashMap while the integer array holds the IDs of the resources in your layout. The system maps the content of the HashMap to the layout resources based on the index of the two arrays.
So I did that but my app just crashes before the view even loads completely. I tried to do it with only one "put" for "row". So I removed all relation with icon and tried just with text, still same. Can you please see what I am doing wrong. I've updated my original post.
I figured it out. I have to use JavaList<IDictionary and JavaDictionary for each item I add, not Dictionary. Basically, ArrayList is JavaList and HashMap is JavaDictionary. Once again, thanks you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.