From: http://www.eoeandroid.com/thread-64029-1-1.html
I want to create a ListView that combines icons and text. The icon data is also written in xml (see arrays. xml below ). However, the output shows that
@ Drawable/latest is not the id value, but the path value of the image. How can I store it? Previously, map. put ("icon ",
R. drawable. XXX); you can bind the icon...
The brief code is as follows:
- String [] iconArr = getResources (). getStringArray (R. array. feed_icons );
- String [] titleArr = getResources (). getStringArray (R. array. feed_names );
- For (int I = 0; I <iconArr. length; I ++)
- {
- Map = new HashMap <String, Object> ();
- ImageView v = new ImageView (getContext ());
- Map. put ("icon", iconArr [I]); // This write is wrong: (how should I write it?
- Map. put ("title", titleArr [I]);
- List. add (map );
- }
Copy code
Arrays. xml is as follows:
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Resources>
- <String-array name = "feed_names">
- <Item> News </item>
- <Item> video </item>
- <Item> international news </item>
- <Item> sports </item>
- <Item> art </item>
- <Item> catering </item>
- </String-array>
- <String-array name = "feed_icons">
- <Item> @ drawable/latest </item>
- <Item> @ drawable/video </item>
- <Item> @ drawable/world </item>
- <Item> @ drawable/sports </item>
- <Item> @ drawable/arts </item>
- <Item> @ drawable/dining </item>
- </String-array>
- </Resources>
Copy code
Solution:
Special thanks to LuoYer
- Map <String, Object> map;
- TypedArray ta = getResources (). obtainTypedArray (R. array. feed_icons );
- String [] titleArr = getResources (). getStringArray (R. array. feed_names );
- For (int I = 0; I <titleArr. length; I ++)
- {
- Map = new HashMap <String, Object> ();
- Map. put ("icon", ta. getResourceId (I, 0 ));
- Map. put ("title", titleArr [I]);
- List. add (map );
- }
Copy code
======================================Private void initAdapters (Context context ){
TypedArray mainNavIcon = context. getResources (). obtainTypedArray (R. array. mainNavIcon );
String [] mainNav = context. getResources (). getStringArray (R. array. mainNav );
MHomeAdapter = new PopupIconTextAdapter (context );
For (int I = 0; I <mainNav. length; I ++ ){
MHomeAdapter. add (getIconTextListItem (context, mainNavIcon. getResourceId (I, 0), mainNav [I]);
}
}
Private IconTextListItem getIconTextListItem (Context context, int mainNavIcon, String mainNavText ){
IconTextListItem iconText = new IconTextListItem (context. getResources (). getDrawable (
MainNavIcon), mainNavText );
Return iconText;
}