Sep 27

I don't know it it's my searching skills which are fading or the lack of examples, but I wasn't able to find a simple example on how to use the simple_list_item_2 which is built into Android. I've used some custom layouts to establish the same result, and sometimes some extras (display an icon on the right for example). However, now I wanted to use the built-in feature, but had to search quite some time. If you experience the same, hopefully this post will save you time.

I know: it's basic, but I'm still learning Android, so I think I may behave like a n00b at the moment ;-)

So, what do we need for setting up the list:

  • screen layout
  • activity
  • list content

Let's go! First create a layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/screen_about_layout"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical"
	xmlns:android="http://schemas.android.com/apk/res/android"
>
	<RelativeLayout
		android:id="@+id/screen_title_bar"
		android:layout_width="fill_parent"
		android:layout_height="60px"
		android:layout_alignParentTop="true"
		android:layout_alignParentLeft="true"
		android:background="@drawable/title_bar"
	>
		<TextView
			android:id="@+id/screen_top_title"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="@string/screen_more_title"
			android:layout_centerVertical="true"
			android:layout_alignParentLeft="true"
			android:textSize="18sp" 
			android:textColor="@color/white"
			android:paddingLeft="10px"
		>
		</TextView>
		<ImageButton
			android:id="@+id/screen_top_info"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:src="@drawable/icon_info"
			android:layout_centerVertical="true"
			android:layout_alignParentRight="true"
			android:paddingRight="10px"
			android:background="@null"
		>
		</ImageButton>	
	</RelativeLayout> 

<!-- Set height to 0, and let the weight param expand it -->
    <!-- Note the use of the default ID! This lets us use a 
         ListActivity still! -->
    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" 
         /> 

</LinearLayout>

Note that the list has an id called '@android:id/list'. This enables us to directly use the list in the Activity as it's going to extend ListActivity.

In the Activity itself we define an ArrayList containing HashMaps from String to String. Basically this means that we create a list which will contain key, value pairs which we are going to control (This is similar to associative arrays in PHP). We will use the key 'line1' for the upper line and 'line2' for the lower line. With the variables from and to we map the values of the keys we supply (this is the from variable) to the TextViews of the simple_list_item_row_2 (the to variable). These are called text1 and text2.

package com.example.sampleapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class MoreScreen extends ListActivity {

	ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(
			2);

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_more);

		HashMap<String, String> map;

		map = new HashMap<String, String>();
		map.put("line1", "Foo");
		map.put("line2", "Bar");
		list.add(map);

		map = new HashMap<String, String>();
		map.put("line1", "Hi");
		map.put("line2", "Bye");
		list.add(map);

		// the from array specifies which keys from the map
		// we want to view in our ListView
		String[] from = { "line1", "line2" };

		// the to array specifies the TextViews from the xml layout
		// on which we want to display the values defined in the from array
		int[] to = { android.R.id.text1, android.R.id.text2 };

		// create the adapter and assign it to the listview
		SimpleAdapter adapter = new SimpleAdapter(this, list,
				android.R.layout.simple_list_item_2, from, to);
		setListAdapter(adapter);

	}

}

One Response to “Using SimpleAdapter for populating simple_list_item_2 with Strings”

  1. rafe says:

    This a great example. Can you please give an example of the code that selects an item from the list?
    thanks

Leave a Reply


nine + 8 =

preload preload preload