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
- list content
Let’s go! First create a layout:
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.