[Android UI] combination of ListView and arrays. xml

Source: Internet
Author: User

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:

  1. String [] iconArr = getResources (). getStringArray (R. array. feed_icons );
  2. String [] titleArr = getResources (). getStringArray (R. array. feed_names );
  3. For (int I = 0; I <iconArr. length; I ++)
  4. {
  5. Map = new HashMap <String, Object> ();
  6. ImageView v = new ImageView (getContext ());
  7. Map. put ("icon", iconArr [I]); // This write is wrong: (how should I write it?
  8. Map. put ("title", titleArr [I]);
  9. List. add (map );
  10. }

Copy code


Arrays. xml is as follows:

  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Resources>
  3. <String-array name = "feed_names">
  4. <Item> News </item>
  5. <Item> video </item>
  6. <Item> international news </item>
  7. <Item> sports </item>
  8. <Item> art </item>
  9. <Item> catering </item>
  10. </String-array>
  11. <String-array name = "feed_icons">
  12. <Item> @ drawable/latest </item>
  13. <Item> @ drawable/video </item>
  14. <Item> @ drawable/world </item>
  15. <Item> @ drawable/sports </item>
  16. <Item> @ drawable/arts </item>
  17. <Item> @ drawable/dining </item>
  18. </String-array>
  19. </Resources>

Copy code

Solution:
Special thanks to LuoYer

  1. Map <String, Object> map;
  2. TypedArray ta = getResources (). obtainTypedArray (R. array. feed_icons );
  3. String [] titleArr = getResources (). getStringArray (R. array. feed_names );
  4. For (int I = 0; I <titleArr. length; I ++)
  5. {
  6. Map = new HashMap <String, Object> ();
  7. Map. put ("icon", ta. getResourceId (I, 0 ));
  8. Map. put ("title", titleArr [I]);
  9. List. add (map );
  10. }
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;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.