views:

412

answers:

1

Hi everybody,

I started learning android development. I'm following a todolist example from a book. Here is a piece of code:

// Create the array list of to do items
final ArrayList<String> todoItems = new ArrayList<String>();

// Create the array adapter to bind the array to the listView
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(  this, 
                                android.R.layout.simple_list_item_1,
                                todoItems
                            );
myListView.setAdapter(aa);

I can't understand exactly this code especially this line:

android.R.layout.simple_list_item_1

Thank you,

Regards

+2  A: 

Zakaria, that is a reference to an built-in XML layout document that is part of the Android OS, rather than one of your own XML layouts.

Here is a further list of layouts that you can use: http://developer.android.com/reference/android/R.layout.html

Also, if you go here: http://www.netmite.com/android/mydroid/frameworks/base/core/res/res/layout/

You can actually view the code for the layouts.

kcoppock
The layouts are also in your SDK installation
CommonsWare
Heh, so they are. :P I'd tried looking for them before just by browsing the android jar within Eclipse and it just told me "Source Not Found". But yeah, they're under platforms > android-x > data > res > layout. Good call. :)
kcoppock
Thanks for the answer. But what's the purpose to call this layout in an ArrayAdapter?
Zakaria
It tells the listview what layout to use for the individual rows. There are others with checkedtextviews for multiple selections, you can make custom layouts that include images, and multiple textviews for example. These android.R ones are just some easy to use, already created resources for you.
kcoppock
Thanks! Wow, that's a lot of layouts. All the Android Reference seems to reveal about them (in R.layout.html) are the constant values for each id. Might there be any documentation that _illustrates_ each of these with a sample use case? (e.g., "Layout X looks like this [figure with field ids]. It is best used in cases a, b, and c. It can be seen in action in app Y.") Yes, it _is_ great to know I can plunder the vaults and hack this all out on my own, but a scannable list of illustrations (vs XML) would be such a big help!
Joe D'Andrea
That would be handy, I've not seen one as of yet though. Best bet would be to just keep swapping them out in your code, and running (painfully slow, I know :P)
kcoppock