Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawable getResources().getIdentifier problem

Tags:

android

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.

I have three arrays of strings:

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"};

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?

like image 440
NickOpris Avatar asked Jul 18 '10 19:07

NickOpris


1 Answers

What if you just do this:

user.put ( "icon", getResources().getIdentifier(uri, "drawable", getPackageName()));
like image 97
Cristian Avatar answered Sep 25 '22 04:09

Cristian