c# - Passing image into Android ListView ArrayAdapter -
i have listview
, passing value list using arrayadapter. did.
first created string array:
private static readonly string[] menuitemnames = new string[] { "home", "new", "delete", "submit", "help" };
and created arrayadapter:
this.mymenulist.adapter = new arrayadapter<string> (this, resource.layout.item_menu, resource.id.menuitemtext, menuitemnames);
and seems work fine. values menuitemnames array goes list , makes new item. next each item, have icon. how can set icon of each one?
this 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 make new arrayadapter:
this.mymenulist.adapter = new arrayadapter<drawable> (this, resource.layout.item_menu, resource.id.imgview, sidemenuitemiconsdrawables);
this doesn't seem work. nee pass instead of drawable change image of imageview
?
thank time.
// ===========================================================
edit: tried. crashes app before view loads. adapter count changes 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;
there several ways of solving that, me think using simpleadapter simplest way. make list of hashmap of string , object. create layout, populate list text , icons wish use , pass layout , list simpleadapter constructor. 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[] = {r.id.sidemenuitemiconsdrawables, r.id.menuitemtext}; //ids of text , icon string[] = {"icon", "text"}; adapter = new simpleadapter(this, menuitemnames, r.layout.item_menu, from, to); listview.setadapter(adapter);
i hope helps.
Comments
Post a Comment