gpt4 book ai didi

c# - 将图像传递到 Android ListView ArrayAdapter

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:38 24 4
gpt4 key购买 nike

我有一个 ListView,我正在使用 ArrayAdapter 将值传递给这个列表。这就是我所做的。

首先我创建了字符串数组:

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

然后创建ArrayAdapter:

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

这似乎工作正常。 menuItemNames 数组中的值进入列表并创建一个新项目。但是在每个项目旁边,我都有一个图标。如何设置每个图标?

这是我尝试过的:

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);

然后,制作可绘制数组:

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

然后创建一个新的 ArrayAdapter:

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

这似乎行不通。我需要传递什么而不是 drawable 来更改 ImageView 的图像?

感谢您的宝贵时间。

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

编辑:所以这就是我尝试过的。但它甚至在 View 加载之前就让我的应用程序崩溃了。适配器数量发生变化,但在那之后它就崩溃了。

        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;

最佳答案

有几种方法可以解决这个问题,对我来说,我认为使用 SimpleAdapter 是最简单的方法。制作字符串和对象的 HashMap 列表。创建您的布局,使用您希望使用的文本和图标填充您的列表,并将布局和列表传递给您的 SimpleAdapter 构造函数。像这样的东西:

    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);

希望对您有所帮助。

关于c# - 将图像传递到 Android ListView ArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27022294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com