DemoActivityListItem: Simple icon text list, and more...

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

DemoActivityListItem: Simple icon text list, and more...

Postby Anm » Wed Oct 15, 2008 10:04 am

I've had so much fun with the examples on this site in on my first day that I decided to give back to the community.

The following is an annotated example of how to make a really simple static list with icons. Additionally, is uses the the pre-built layouts provided with Android to maximize a compatible look and feel, while minimizing your efforts. This makes it much simpler than the IconifiedTextList.

I've attached a screenshot below.

What you'll learn:
    * Using runtime reflection to explore Android classes and catalog resource constants (the data we'll display).
    * Using the ListActivity to handle the boilerplate of list activities.
    * Using the SimpleListAdapter to populate your lists with static data
    * Using the android.R.layout.activity_list_item for text with icons



But first we have to have some data to look at. The first file is a little utility class that itemizes some of the resources that come with Android. It uses runtime reflection on the android.R.* classes, building a SortedMap of names to numeric resource ids.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. /*  AndroidResources.java
  3.  
  4.  *
  5.  
  6.  *  Public Domain
  7.  
  8.  *  Copyright 2008 - All rights released
  9.  
  10.  */
  11.  
  12. package org.anddev.android.tutorial;
  13.  
  14.  
  15.  
  16. import java.lang.reflect.Field;
  17.  
  18. import java.lang.reflect.Modifier;
  19.  
  20. import java.util.Collections;
  21.  
  22. import java.util.SortedMap;
  23.  
  24. import java.util.TreeMap;
  25.  
  26.  
  27.  
  28. import android.util.Log;
  29.  
  30.  
  31.  
  32. /**
  33.  
  34.  *  Using reflection, name maps of the resources available in android.R
  35.  
  36.  */
  37.  
  38. public class AndroidResources {
  39.  
  40.         private static final String TAG = "AndroidResources";
  41.  
  42.        
  43.  
  44.         /** SortedMap of android.R.id constants to their values. */
  45.  
  46.         public static SortedMap< String, Integer > IDS_NAMES;
  47.  
  48.         /** SortedMap of android.R.drawable resource id constants to their values. */
  49.  
  50.         public static SortedMap< String, Integer > DRAWABLE_NAMES;
  51.  
  52.        
  53.  
  54.         static {
  55.  
  56.                 // Populate the map when this class is first referenced
  57.  
  58.                 IDS_NAMES      = mapIdConstants( android.R.id.class );
  59.  
  60.                 DRAWABLE_NAMES = mapIdConstants( android.R.drawable.class );
  61.  
  62.         }
  63.  
  64.  
  65.  
  66.     /**
  67.  
  68.      *  For a given class, build a map of all public int constants (i.e., static fields).
  69.  
  70.      *  
  71.  
  72.      *  Uses the Java reflection APIs.
  73.  
  74.      */
  75.  
  76.     private static SortedMap< String, Integer > mapIdConstants( Class<? extends Object> c ) {
  77.  
  78.         SortedMap<String,Integer> map = new TreeMap<String,Integer>();
  79.  
  80.  
  81.  
  82.         Field fields[] = c.getFields();  // A constant is a type of field
  83.  
  84.         for( Field f : fields ) {
  85.  
  86.                 // Look for the fields matching prototype "public static int"
  87.  
  88.                 int modifiers = f.getModifiers();
  89.  
  90.                 if( Modifier.isPublic( modifiers ) &&
  91.  
  92.                 Modifier.isStatic( modifiers ) &&
  93.  
  94.                         f.getType().equals( Integer.TYPE ) )  // Integer.TYPE is the class for the int primitive
  95.  
  96.                 {
  97.  
  98.                         String name = f.getName();  // Get the field name
  99.  
  100.                         try {
  101.  
  102.                     int value = f.getInt( null );  // And its value
  103.  
  104.                    
  105.  
  106.                     // Add it to the map
  107.  
  108.                     map.put( name, value );
  109.  
  110.                 } catch( Exception error ) {
  111.  
  112.                         Log.e( TAG, "Failed to access id constant "+c.getName()+"."+name+'\n'+error );
  113.  
  114.                 }
  115.  
  116.                 }
  117.  
  118.         }
  119.  
  120.        
  121.  
  122.             return Collections.unmodifiableSortedMap( map );
  123.  
  124.     }
  125.  
  126. }
  127.  
  128.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Now we come to the heart of the tutorial: the text list.

Extending from ListActivity gives us a ListView for free. While I don't demo it here, we can still customize it with the usual call to setContentView(..), as long as the view contains one ListView with the id android.R.id.list (a.k.a. @android:id/list).

Since our data is static, we can use the SimpleListAdapter. It's not as efficient or flexible as the other adapters or a custom adapter, but it does what we need in a pinch.

That said, the simple adapters require data be formatted as nested collections. The SimpleListadapter requires the data in a List<Map<String,Object>> format. So the buildListForSimpleAdapter() method reformats our data for us.

Secondly, the SimpleListAdapter builds its item layouts by looking at the ids of the component (I assume via findViewById(..)). Thus we have to match the ids to those specified in Android's built-in layout. For activity_list_item, those fields are android.R.id.text1 and android.R.id.icon (the last argument of the SimpleListAdapter's constructor). If you decide to customize your layout, you can use the same ids, or replace the ids specified here with your own.

All together, we have a compete iconified text list without any custom list adapters or layouts.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. /*  DemoActivityListItem.java
  3.  
  4.  *
  5.  
  6.  *  Public Domain
  7.  
  8.  *  Copyright 2008 - All rights released
  9.  
  10.  */
  11.  
  12. package org.anddev.android.tutorial;
  13.  
  14.  
  15.  
  16. import java.util.ArrayList;
  17.  
  18. import java.util.HashMap;
  19.  
  20. import java.util.List;
  21.  
  22. import java.util.Map;
  23.  
  24. import java.util.SortedMap;
  25.  
  26.  
  27.  
  28. import android.os.Bundle;
  29.  
  30. import android.util.Log;
  31.  
  32. import android.widget.SimpleAdapter;
  33.  
  34. import android.app.ListActivity;
  35.  
  36.  
  37.  
  38. /**
  39.  
  40.  *  Demonstrates the built-in Android layout activity_list_item.
  41.  
  42.  *  
  43.  
  44.  *  Utilizes the built-in icons as source material for the demo list.
  45.  
  46.  *  These are collected via reflection.
  47.  
  48.  */
  49.  
  50. public class DemoActivityListItem extends ListActivity {
  51.  
  52.         ///////////////////////////////////////////////////////////////////////
  53.  
  54.         //  Private Constants
  55.  
  56.        
  57.  
  58.         /** Tag for logging */
  59.  
  60.         private static final String TAG = "DemoActivityListItem";
  61.  
  62.  
  63.  
  64.         /** Attribute key for the list item text. */
  65.  
  66.     private static final String LABEL = "LABEL";
  67.  
  68.     /** Attribute key for the list item icon's drawable resource. */
  69.  
  70.     private static final String ICON  = "ICON";
  71.  
  72.  
  73.  
  74.        
  75.  
  76.         ///////////////////////////////////////////////////////////////////////
  77.  
  78.         //  Public Methods
  79.  
  80.     /** Called when the activity is first created. */
  81.  
  82.     @Override
  83.  
  84.     public void onCreate( Bundle savedInstanceState ) {
  85.  
  86.         super.onCreate(savedInstanceState);
  87.  
  88.         Log.i( TAG, "Entered onCreate(..)" );
  89.  
  90.        
  91.  
  92.         // Format the data for the SimpleListAdapter:
  93.  
  94.         // Each item in the list represents one list entry.
  95.  
  96.         // The attributes of this item are represented in a Map,
  97.  
  98.         // with the names of the attributes as the keys.
  99.  
  100.         // Our keys are LABEL and ICON.
  101.  
  102.         List< Map<String,Object> > drawables = buildListForSimpleAdapter();
  103.  
  104.        
  105.  
  106.         // Now build the list adapter
  107.  
  108.         SimpleAdapter adapter = new SimpleAdapter(
  109.  
  110.                 // the Context
  111.  
  112.                 this,
  113.  
  114.                 // the data to display
  115.  
  116.                 drawables,
  117.  
  118.                 // The layout to use for each item
  119.  
  120.             android.R.layout.activity_list_item,
  121.  
  122.             // The list item attributes to display
  123.  
  124.             new String[] { LABEL, ICON },
  125.  
  126.             // And the ids of the views where they should be displayed (same order)
  127.  
  128.             new int[] { android.R.id.text1, android.R.id.icon }
  129.  
  130.         );
  131.  
  132.  
  133.  
  134.         setListAdapter( adapter );
  135.  
  136.  
  137.  
  138.         Log.i( TAG, "Exiting onCreate(..)" );
  139.  
  140.     }
  141.  
  142.  
  143.  
  144.         /**
  145.  
  146.      * @return
  147.  
  148.      */
  149.  
  150.     private List< Map<String,Object> > buildListForSimpleAdapter() {
  151.  
  152.         // Data source...
  153.  
  154.         SortedMap<String,Integer> drawables = AndroidResources.DRAWABLE_NAMES;
  155.  
  156.         // Resulting list...
  157.  
  158.         List< Map<String,Object> > list = new ArrayList< Map<String,Object> >( drawables.size() );
  159.  
  160.        
  161.  
  162.         for( String name : drawables.keySet() ) {
  163.  
  164.                 // For each item in the source data
  165.  
  166.                 int id = drawables.get( name );
  167.  
  168.                
  169.  
  170.                         // Build a map for the attributes
  171.  
  172.                 Map<String,Object> map = new HashMap<String,Object>();
  173.  
  174.                 map.put( LABEL, name );
  175.  
  176.                 map.put( ICON, id );
  177.  
  178.                 list.add( map );
  179.  
  180.         }
  181.  
  182.        
  183.  
  184.             return list;
  185.  
  186.     }
  187.  
  188. }
  189.  
  190.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
Attachments
DemoActivityListItem.png
Screenshot of DemoActivityListItem
DemoActivityListItem.png (81.1 KiB) Viewed 19518 times
Anm
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 15, 2008 8:21 am

Top

Postby maax » Tue Dec 16, 2008 8:01 am

Exactly what I needed to get going!

Thanks!
maax
Freshman
Freshman
 
Posts: 2
Joined: Sun Dec 14, 2008 9:21 pm

Postby maax » Wed Dec 24, 2008 3:54 am

After checking out this tut you might want to replace the SimpleAdapter with an adapter which extends BaseAdapter. This adapter gives you a lot more control.
maax
Freshman
Freshman
 
Posts: 2
Joined: Sun Dec 14, 2008 9:21 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests