绘制getResources()。则getIdentifier问题 [英] drawable getResources().getIdentifier problem

查看:93
本文介绍了绘制getResources()。则getIdentifier问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次来这里(我发现网站非常有用)。我是新的Andr​​oid和我需要找出我为什么不能动态地在我的列表视图中加载图像。

this is my first time here (I find the website very useful). I'm new to android and I need to find out why I can't dynamically load images in my list view.

我有三个字符串数组:

private String lv_arr[]={"News", 
        "Events", 
        "Other"};
private String lv_arr_d[]={"Get the latest news", 
        "Latest events", 
        "Other description here"};
private String lv_arr_icons[]={"news", "events", "other"};

我的列表显示图标的左侧和两行文字中的右侧​​的每个列表项。 它的工作原理确定,当C中的映像的名称(如新闻)我很难$ C $。

My list displays an icon to the left and two lines of text to the right in each list item. It works ok when I hard code the name of the image (e.g. news).

ArrayList <HashMap <String, Object>> users = 
     new ArrayList <HashMap <String, Object>> ();
     int l = lv_arr.length;
     for (int i = 0; i <l; i++) {
     HashMap <String, Object> user = new HashMap <String, Object> ();

     //get icons
     String uri = "drawable/"+lv_arr_icons[i];

     user.put ( "icon", (Drawable)getResources().getDrawable(
             getResources().getIdentifier(uri, null, getPackageName())));
//the above works ok if i set R.drawable.news 
     user.put ( "label", lv_arr[i].toString()); 
     user.put ( "description",  lv_arr_d[i].toString()); 
     users.add (user); 
     }

     String[] names = {"icon", "label", "description"};
     int[] views = {R.id.icon, R.id.label, R.id.description};

     SimpleAdapter saImageItems = new SimpleAdapter(this.getApplicationContext(), 
             users,  R.layout.optionslistitem, names, views);
       lv1.setAdapter(saImageItems); //lv1 is my ListView

所有图像都在绘制文件夹。 任何想法,为什么没有图像加载当我试图通过他们的名字,并从资源,让他们?

All images are in the drawable folder. Any ideas why no image loads when i try to pass on their name and get them from resources?

推荐答案

如果你只是这样做:

user.put ( "icon", getResources().getIdentifier(uri, "drawable", getPackageName()));

这篇关于绘制getResources()。则getIdentifier问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆