There are four precompiled list XML definitions:
simple_list_item_1
simple_list_item_1_small
simple_list_item_2
two_line_list_item
See ListActivity
I can only get the first two to work. The following is taken almost straight from Android examples and works.
I just changed it to use a List instead of creating the ArrayAdapter directly from mStrings.
Using java Syntax Highlighting
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- List<String> items = new ArrayList<String>();
- for ( String s : mStrings )
- items.add( s );
- ArrayAdapter<String> notes =
- new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1_small, items);
- setListAdapter(notes);
- }
- private String[] mStrings = {
- "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
- "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", ...
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
two_line_list_item has the following XML definition
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView id="text1"
- android:textSize="16"
- android:textStyle="bold"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <TextView id="text2"
- android:textSize="16"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
However using it causes a ClassCastException
Using java Syntax Highlighting
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- List<String[]> Ditems = new ArrayList<String[]>();
- for ( String[] sa : mDStrings )
- Ditems.add( sa );
- SimpleAdapter adapter = new SimpleAdapter(
- this,
- Ditems,
- android.R.layout.two_line_list_item,
- new String[] { "text1", "text2" },
- new int[] { android.R.id.text1, android.R.id.text2 } );
- setListAdapter(adapter);
- }
- private String[][] mDStrings = {
- { "Cheese", "Abbaye de Belloc" },
- { "Wine", "Caubernet Sauvignon" },
- { "Beer", "Guiness" }
- };
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Its really not clear what are the last two parameters "From" and "To" in the SimpleAdapter constructor
There is an example in the ListActivity documentation using a SimpleCursorAdapter however I can't get it to compile
and in any case, don't want to load the data from an android database, but use my own data.
Using java Syntax Highlighting
- public class MyListAdapter extends ListActivity {
- @Override
- protected void onCreate(Bundle icicle){
- super.onCreate(icicle);
- // We'll define a custom screen layout here (the one shown above), but
- // typically, you could just use the standard ListActivity layout.
- setContentView(R.layout.custom_list_activity_view);
- // Query for all people contacts using the Contacts.People convenience class.
- // Put a managed wrapper around the retrieved cursor so we don't have to worry about
- // requerying or closing it as the activity changes state.
- mCursor = People.query(this.getContentResolver(), null);
- startManagingCursor(mCursor);
- // Now create a new list adapter bound to the cursor.
- // SimpleListAdapter is designed for binding to a Cursor.
- ListAdapter adapter = new SimpleCursorAdapter(
- this, // Context.
- android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor
- rows).
- mCursor, // Pass in the cursor to bind to.
- new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
- new int[]); // Parallel array of which template objects to bind to those columns.
- // Bind to our new adapter.
- setListAdapter(adapter);
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

