Multiple row or cloumn lists

Put your problem here if it does not fit any of the other categories.

Multiple row or cloumn lists

Postby francis » Mon Mar 03, 2008 3:28 pm

I cannot get any list types other than the single row, single column: simple_list_item_1, simple_list_item_1_small to work.

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.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     public void onCreate(Bundle icicle) {
  3.  
  4.         super.onCreate(icicle);
  5.  
  6.  
  7.  
  8.         List<String> items = new ArrayList<String>();
  9.  
  10.        
  11.  
  12.         for ( String s : mStrings )
  13.  
  14.                 items.add( s );
  15.  
  16.        
  17.  
  18.         ArrayAdapter<String> notes =
  19.  
  20.             new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1_small, items);
  21.  
  22.         setListAdapter(notes);
  23.  
  24.     }
  25.  
  26.  
  27.  
  28.    private String[] mStrings = {
  29.  
  30.             "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
  31.  
  32.             "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", ...
  33.  
  34.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


two_line_list_item has the following XML definition

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout
  5.  
  6.    android:layout_width="fill_parent"
  7.  
  8.    android:layout_height="wrap_content"
  9.  
  10.    android:orientation="vertical">
  11.  
  12.  
  13.  
  14.     <TextView id="text1"
  15.  
  16.        android:textSize="16"
  17.  
  18.        android:textStyle="bold"
  19.  
  20.        android:layout_width="fill_parent"
  21.  
  22.        android:layout_height="wrap_content"/>
  23.  
  24.  
  25.  
  26.     <TextView id="text2"
  27.  
  28.        android:textSize="16"
  29.  
  30.        android:layout_width="fill_parent"
  31.  
  32.        android:layout_height="wrap_content"/>
  33.  
  34. </LinearLayout>
  35.  
  36.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


However using it causes a ClassCastException

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     public void onCreate(Bundle icicle) {
  3.  
  4.         super.onCreate(icicle);
  5.  
  6.  
  7.  
  8.         List<String[]> Ditems = new ArrayList<String[]>();
  9.  
  10.        
  11.  
  12.         for ( String[] sa : mDStrings )
  13.  
  14.                 Ditems.add( sa );
  15.  
  16.        
  17.  
  18.         SimpleAdapter adapter = new SimpleAdapter(
  19.  
  20.                         this,
  21.  
  22.                         Ditems,
  23.  
  24.                         android.R.layout.two_line_list_item,
  25.  
  26.                         new String[] { "text1", "text2" },
  27.  
  28.                         new int[] { android.R.id.text1, android.R.id.text2 } );
  29.  
  30.        
  31.  
  32.         setListAdapter(adapter);
  33.  
  34.     }
  35.  
  36.  
  37.  
  38.     private String[][] mDStrings = {
  39.  
  40.                 { "Cheese", "Abbaye de Belloc" },
  41.  
  42.                 { "Wine",   "Caubernet Sauvignon" },
  43.  
  44.                 { "Beer",   "Guiness" }
  45.  
  46.     };
  47.  
  48.  
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.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MyListAdapter extends ListActivity {
  3.  
  4.  
  5.  
  6.     @Override
  7.  
  8.     protected void onCreate(Bundle icicle){
  9.  
  10.         super.onCreate(icicle);
  11.  
  12.  
  13.  
  14.         // We'll define a custom screen layout here (the one shown above), but
  15.  
  16.         // typically, you could just use the standard ListActivity layout.
  17.  
  18.         setContentView(R.layout.custom_list_activity_view);
  19.  
  20.  
  21.  
  22.         // Query for all people contacts using the Contacts.People convenience class.
  23.  
  24.         // Put a managed wrapper around the retrieved cursor so we don't have to worry about
  25.  
  26.         // requerying or closing it as the activity changes state.
  27.  
  28.         mCursor = People.query(this.getContentResolver(), null);
  29.  
  30.         startManagingCursor(mCursor);
  31.  
  32.  
  33.  
  34.         // Now create a new list adapter bound to the cursor.
  35.  
  36.         // SimpleListAdapter is designed for binding to a Cursor.
  37.  
  38.         ListAdapter adapter = new SimpleCursorAdapter(
  39.  
  40.                 this, // Context.
  41.  
  42.                 android.R.layout.two_line_list_item,  // Specify the row template to use (here, two columns bound to the two retrieved cursor
  43.  
  44. rows).
  45.  
  46.                 mCursor,                                    // Pass in the cursor to bind to.
  47.  
  48.                 new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
  49.  
  50.                 new int[]);                                 // Parallel array of which template objects to bind to those columns.
  51.  
  52.  
  53.  
  54.         // Bind to our new adapter.
  55.  
  56.         setListAdapter(adapter);
  57.  
  58.     }
  59.  
  60. }
  61.  
  62.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
francis
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Feb 09, 2008 6:33 am
Location: Germany

Top

Re: Multiple row or cloumn lists

Postby paller » Mon Mar 17, 2008 12:02 am

francis wrote:I cannot get any list types other than the single row, single column: simple_list_item_1, simple_list_item_1_small to work.
[/syntax]


Check out this example program.
paller
Developer
Developer
 
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London

Good link

Postby francis » Tue Mar 18, 2008 3:19 pm

Thanks Paller, that link was really useful. I though I get Email notifications about posts to this thread, but must have overlooked it, so only saw it last night, and got his sample code working.
francis
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Feb 09, 2008 6:33 am
Location: Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 10 guests