Beruflich Dokumente
Kultur Dokumente
com
search linux.com
In our previous Android coding tutorials (part 1, part 2), you set up your dev environment, built a basic app,
and then improved it by adding a menu and a second Activity. In this tutorial we're going to look at a very
handy part of the Android API: ListView, ListActivity, and the associated methods which give you an easy
way to show the user a list and then act when they click a list item.
Creating a ListView
A very common pattern in an Android activity is showing a list of items for the user to select from. The
Android API provides the ListView and ListActivity classes to help out with this. Carrying on with the Latest Documentation
Countdown app from previous tutorials, we'll list a few sample countdown times for the user to select
from to set the timer. 10 Lesser Know n Commands for Linux – Part 3
} DETAILS
The problem with this layout is that it only shows a list. We want to be able to see the countdown and the AlexaMobile @Alexa_Movil 4m
start button as well. This means setting up our own XML layout, rather than relying on ListActivity to TimeShift: Restore Your Linux
generate its own layout. Add this to your XML, below the TextView and the Button: Desktops To Previous State
ift.tt/1hoUiBX #Linux
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content" CleanVPS @CleanVPS 4m
android:layout_height="wrap_content" For a limited time, XEN VPS Only
android:layout_below="@+id/startbutton" /> $3.30/month. goo.gl/dcgeR
Unbelievable! I know! #vps
#webhosting #xen #linux
It's important that the ListView should have the ID @android:id/list. This is what enables the
ListActivity to do its magic without you explicitly setting up the List.
www.linux.com/learn/docs/690708-android-programming-multiple-choice-lists 1/4
30/11/13 Android Programming for Beginners: User Menus | Linux.com
First Nam e
Last Nam e
Em ail
Country
Now go back to CountdownActivity.onCreate(), and put your previous display and button setup code Subscribe
back in, after the ListView setup: View our Privacy Policy
Again, it's important that you set up the ListView first, before setContentView(), or it won't work properly.
Recompile and run, and you'll see the list appear below the text and button. What you won't see, though, is
anything happening when you click the list elements. The next section will tackle that problem.
One final note: you can also set up an empty element in the layout, which will display if and only if the
ListView is empty. Add this to your XML:
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/startbutton"
android:text="@string/empty_list" />
(You'll need to set up the string value in res/values/strings.xml, too). Now replace the array
declaration line in CountdownActivity.onCreate()with this one:
Compile and run, and you'll see the empty text displayed, and no list. Put the array declaration back how it
was, compile and run again, and the list shows, but no text. In our app this isn't particularly useful, but if
you were populating the array from elsewhere in your code, it's a neat trick to have available.
This is all pretty self-explanatory! We grab the ListView, set its OnItemClickListener, and create an
www.linux.com/learn/docs/690708-android-programming-multiple-choice-lists 2/4
30/11/13 Android Programming for Beginners: User Menus | Linux.com
onItemClick()method for the Listener. As you can see here, onItemClick()has access to the
position in the List of the item you clicked on. So we can grab the ListAdapter, get the item from that
position, and then cast the value to an Integer. Save and run, and you have a list of values to set your
timer.
<ListView ....
android:choiceMode="singleChoice"
... >
Run it again, and the selection indicator does its job. If you were using a ListActivity without an XML file,
you could do this with a line of code in your app:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Conclusion
There are lots of situations in apps where you might want to show the user a list. ListView and ListActivity
make that very easy, and as shown just above, there are plenty of ways to improve the UI experience. You
could also look at providing a context menu (in these tutorials we've only used the options menu so far)
when the user long-clicks on a list item. Or you could look at some form of back-end data storage, and
allow the user to add and edit their own list items, so they have a list of countdown times that they
regularly use. As ever, keep playing with the code and see where it takes you!
For more Android programming training resources, please visit the Linux training website.
Juliet Kemp
Comments
DavidChipman : 07 Feb
I see you still persist in not including the required java import statements at the top
of the java files. I suppose your laziness could be explained by the fact that Eclipse
will offer to add them for you. But really, as code examples, these suck big time!
Some beginners tutorial! Idiot!
Report Reply
Report Reply
Poul : 22 Feb
can you create a special place on the menu or in site where will all the tutorials for
android? Because now is a little chaotic part 1, part 2 now User Menus,... For
example: Linux Documentation>android-part1 -part2...
Report Reply
Al : 11 Mar
This tutorial could use some serious help. You consistently just glaze over the
details that a beginner ready a tutorial would need to complete the sample app. It
just baffles me that you would go through all of the work to put this tutorial together
www.linux.com/learn/docs/690708-android-programming-multiple-choice-lists 3/4
30/11/13 Android Programming for Beginners: User Menus | Linux.com
and only do it 90%. Add in the missing pieces, some source code, and maybe
connect the dots a little better and I WOULD give this 5 stars!
Report Reply
Name :
Email :
Comment :
www.linux.com/learn/docs/690708-android-programming-multiple-choice-lists 4/4