Java Reference
In-Depth Information
Listing 5-4. Code Generating the List Shown in Figure 5-11
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ListExample extends MIDlet {
public void startApp() {
final List l = new List("Pizza Toppings", Choice.MULTIPLE);
l.append("Anchovies", null);
l.append("Cheese", null);
l.append("Olives", null);
l.append("Onions", null);
l.append("Pepperoni", null);
l.append("Sausage", null);
l.addCommand(new Command("Order", "Order Pizza", Command.ITEM, 0));
l.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
boolean isSelected[] = new boolean[ l.size() ];
int i;
l.getSelectedFlags( isSelected );
for(i=0;i<l.size(); i++ ) {
if ( isSelected[i]){
System.out.println( l.getString( i ));
}
}
}
});
Display.getDisplay(this).setCurrent(l);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
}
This code also shows the usual way of getting selections from a Choice subclass:
in the case of multiple lists, simply iterate over a list of boolean values obtained from
Choice.getSelectedFlags . For an exclusive list, it's even easier: just invoke Choice.
getSelectedIndex , because the user can select only a single item.
 
Search WWH ::




Custom Search