【问题标题】:Passing image into Android ListView ArrayAdapter将图像传递到 Android ListView ArrayAdapter
【发布时间】:2014-11-19 16:45:38
【问题描述】:

我有一个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 的图像?

感谢您的宝贵时间。

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

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

        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;

【问题讨论】:

    标签: c# android arrays listview xamarin


    【解决方案1】:

    有几种方法可以解决这个问题,我认为使用 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# lang,所以我必须使用 Dictionary 而不是 HashMap。这是一个有点不同但相同的想法。但我不确定 String[] from 是什么。你能告诉我吗?谢谢
    • 最后两个参数是字符串数组和整数数组,字符串数组保存HashMap中对象的键,而整数数组保存布局中资源的ID。系统根据两个数组的索引将HashMap的内容映射到布局资源中。
    • 所以我这样做了,但我的应用程序在视图完全加载之前就崩溃了。我试图用一个“行”的“放置”来做到这一点。所以我删除了与图标的所有关系并尝试使用文本,仍然相同。你能看看我做错了什么吗?我已经更新了我原来的帖子。
    • 我想通了。我必须为我添加的每个项目使用JavaList&lt;IDictionaryJavaDictionary,而不是字典。基本上,ArrayListJavaListHashMapJavaDictionary。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多