Sie sind auf Seite 1von 4

30/11/13 Android Programming for Beginners: User Menus | Linux.

com

Linux Foundation Training Events Video          

search linux.com

Home News Linux Community Learn Linux Directory Jobs

Home Learn Linux Linux Documentation Android Programming for


Beginners: User Menus

Android Programming for Beginners: User Menus


Wednesday, 16 January 2013 08:21 Juliet Kemp

Like 109 Tw eet 41 44

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

How to Install and Configure OpenSSH Server In Linux


If all you want is a List, ListActivity will set your View up for you automatically; no need to write any XML at
all. So onCreate()can be very simple: 10 Lesser Know n Linux Commands – Part 2

Using DSH (Distributed Shell) to Run Linux Commands Across


public class CountdownActivity extends ListActivity {
Multiple Machines
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); Advanced Copy Command – Show s Progress Bar While Copying
Large Files/Folders in Linux
Integer[] values = new Integer[] { 5, 10, 15, 20, 25, 30, 45, 60 };
ArrayAdapter adapter =
new ArrayAdapter(this, android.R.layout.simple_list_item_1, Upcoming Training Courses
values);
LF331 Developing Linux Device Drivers
setListAdapter(adapter);
} 09 Dec » 13 Dec - Virtual

} DETAILS

LF426 Linux Perform ance Tuning


CountdownActivity now extends ListActivity. ListActivity does a lot of the preparation work for you, so to 09 Dec » 12 Dec - Virtual
show a list, you just need to create an array of values to show, hook it up to an ArrayAdapter, and set the DETAILS
ArrayAdapter as the ListActivity's ListAdapter. The ArrayAdapter has three parameters:
LF342 Linux Netw ork Managem ent
1. The current context (this); 20 Jan » 23 Jan - Virtual
2. The layout resource defining how each array element should be displayed; DETAILS

3. The array itself (values).


View All Upcom ing Courses
For the layout resource, we're use a standard Android resource,
android.R.layout.simple_list_item_1. But you could create your own, or use another of the
standard layout items (of which more later). You can also take a look at the XML of the standard
Tweets
resources.

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.

Silviu Stahie @thesilviu 8m


#Linux Kernel 3.11.10 Reaches
End of Life, Upgrade to 3.12.x
news.softpedia.com/news/Linux-Ker…

Compose new Tweet…

www.linux.com/learn/docs/690708-android-programming-multiple-choice-lists 1/4
30/11/13 Android Programming for Beginners: User Menus | Linux.com

Sign Up For the Linux.com Newsletter

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

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

Integer[] values .... etc ...


[ ... ]
setListAdapter(adapter);
setContentView(R.layout.activity_main);
countdownDisplay = (TextView) findViewById(R.id.time_display_box);
Button startButton = (Button) findViewById(R.id.startbutton);
[ .... etc .... ]
}

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:

Integer[] values = new Integer[] { };

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.

Clicking on List elements


Now we have the List set up, we need to make it do something when you click on a list element.
Specifically, it should set the countdown seconds to the new value, and change the display. Happily,
Android has a straightforward API for this, too:

ListView list = getListView();


list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id ) {
countdownSeconds = (Integer) getListAdapter().getItem(position);
countdownDisplay.setText(Long.toString(countdownSeconds));
}
});

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.

Changing the List's appearance


Earlier, we mentioned the other standard layouts available. If you switch simple_list_item_1to
simple_list_item_single_choice, and rerun your code, you'll see that you get a selection indicator
next to your list items. However, when you click it, the countdown value changes, but the selection
indicator doesn't do anything. To make this work, you need to change your ListView, too. Add this attribute
in your XML:

<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

Cedric RAVALEC : 12 Feb

You can use AndroVM for testing your app.

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 :

Subscribe to Comments Post Comment

WHO WE ARE ? EXPLORE STAY CURRENT ABOUT LINUX.COM

The Linux Foundation is a non-profit Answ ers Netbooks How to Participate


consortium dedicated to the grow th of
Blogs Cloud Computing Contact / About
Linux.
Forums Enterprise Advertise
More About the foundation...
Directory Embedded & Mobile Privacy / Terms / Editorial Policy
Frequent Questions

Join / Linux Training / Board

Linux.com © 2012 Linux.com. All rights reserved.


The Linux Foundation Symbol is a trademark of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.

www.linux.com/learn/docs/690708-android-programming-multiple-choice-lists 4/4

Das könnte Ihnen auch gefallen