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.
Using java Syntax Highlighting
- /* AndroidResources.java
- *
- * Public Domain
- * Copyright 2008 - All rights released
- */
- package org.anddev.android.tutorial;
- import java.lang.reflect.Field;
- import java.lang.reflect.Modifier;
- import java.util.Collections;
- import java.util.SortedMap;
- import java.util.TreeMap;
- import android.util.Log;
- /**
- * Using reflection, name maps of the resources available in android.R
- */
- public class AndroidResources {
- private static final String TAG = "AndroidResources";
- /** SortedMap of android.R.id constants to their values. */
- public static SortedMap< String, Integer > IDS_NAMES;
- /** SortedMap of android.R.drawable resource id constants to their values. */
- public static SortedMap< String, Integer > DRAWABLE_NAMES;
- static {
- // Populate the map when this class is first referenced
- IDS_NAMES = mapIdConstants( android.R.id.class );
- DRAWABLE_NAMES = mapIdConstants( android.R.drawable.class );
- }
- /**
- * For a given class, build a map of all public int constants (i.e., static fields).
- *
- * Uses the Java reflection APIs.
- */
- private static SortedMap< String, Integer > mapIdConstants( Class<? extends Object> c ) {
- SortedMap<String,Integer> map = new TreeMap<String,Integer>();
- Field fields[] = c.getFields(); // A constant is a type of field
- for( Field f : fields ) {
- // Look for the fields matching prototype "public static int"
- int modifiers = f.getModifiers();
- if( Modifier.isPublic( modifiers ) &&
- Modifier.isStatic( modifiers ) &&
- f.getType().equals( Integer.TYPE ) ) // Integer.TYPE is the class for the int primitive
- {
- String name = f.getName(); // Get the field name
- try {
- int value = f.getInt( null ); // And its value
- // Add it to the map
- map.put( name, value );
- } catch( Exception error ) {
- Log.e( TAG, "Failed to access id constant "+c.getName()+"."+name+'\n'+error );
- }
- }
- }
- return Collections.unmodifiableSortedMap( map );
- }
- }
Parsed in 0.038 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.
Using java Syntax Highlighting
- /* DemoActivityListItem.java
- *
- * Public Domain
- * Copyright 2008 - All rights released
- */
- package org.anddev.android.tutorial;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.SortedMap;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.SimpleAdapter;
- import android.app.ListActivity;
- /**
- * Demonstrates the built-in Android layout activity_list_item.
- *
- * Utilizes the built-in icons as source material for the demo list.
- * These are collected via reflection.
- */
- public class DemoActivityListItem extends ListActivity {
- ///////////////////////////////////////////////////////////////////////
- // Private Constants
- /** Tag for logging */
- private static final String TAG = "DemoActivityListItem";
- /** Attribute key for the list item text. */
- private static final String LABEL = "LABEL";
- /** Attribute key for the list item icon's drawable resource. */
- private static final String ICON = "ICON";
- ///////////////////////////////////////////////////////////////////////
- // Public Methods
- /** Called when the activity is first created. */
- @Override
- public void onCreate( Bundle savedInstanceState ) {
- super.onCreate(savedInstanceState);
- Log.i( TAG, "Entered onCreate(..)" );
- // Format the data for the SimpleListAdapter:
- // Each item in the list represents one list entry.
- // The attributes of this item are represented in a Map,
- // with the names of the attributes as the keys.
- // Our keys are LABEL and ICON.
- List< Map<String,Object> > drawables = buildListForSimpleAdapter();
- // Now build the list adapter
- SimpleAdapter adapter = new SimpleAdapter(
- // the Context
- this,
- // the data to display
- drawables,
- // The layout to use for each item
- android.R.layout.activity_list_item,
- // The list item attributes to display
- new String[] { LABEL, ICON },
- // And the ids of the views where they should be displayed (same order)
- new int[] { android.R.id.text1, android.R.id.icon }
- );
- setListAdapter( adapter );
- Log.i( TAG, "Exiting onCreate(..)" );
- }
- /**
- * @return
- */
- private List< Map<String,Object> > buildListForSimpleAdapter() {
- // Data source...
- SortedMap<String,Integer> drawables = AndroidResources.DRAWABLE_NAMES;
- // Resulting list...
- List< Map<String,Object> > list = new ArrayList< Map<String,Object> >( drawables.size() );
- for( String name : drawables.keySet() ) {
- // For each item in the source data
- int id = drawables.get( name );
- // Build a map for the attributes
- Map<String,Object> map = new HashMap<String,Object>();
- map.put( LABEL, name );
- map.put( ICON, id );
- list.add( map );
- }
- return list;
- }
- }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4

