A list contains one or more choices (elements), which must have a text part,
an optional image part, and an optional font for the text part.
A D V E R T I S E M E N T
The List
element implements the Choice interface, which defines the basic
operations of this element. The list must itself have a title, and must define a
policy for the selection of its elements. This policy dictates whether only one
element can be selected (Choice.EXCLUSIVE), multiple elements can
be selected (Choice.MULTIPLE), or the currently highlighted element
is selected (Choice.IMPLICIT). Figure 4 shows the difference
between the three selection policies.
Figure 4. Selection policies for List elements
You can create a list in one of two ways.
Create an list that contains no elements, and then append or insert
individual elements.
Create the elements beforehand and then create a list with these
elements.
Listing 2 shows both ways.
package com.j2me.part2;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
public class ListExample extends MIDlet {
List fruitList1;
List fruitList2;
public ListExample() {
fruitList1 = new List("Select the fruits you like",
Choice.MULTIPLE);
fruitList1.append("Orange", null);
fruitList1.append("Apple", null);
fruitList1.insert(1, "Mango", null);
// inserts between Orange and Apple
String fruits[] = {"Guava", "Berry", "Kiwifruit"};
fruitList2 =
new List(
"Select the fruits you like - List 2",
Choice.IMPLICIT,
fruits,
null);
}
public void startApp() {
Display display = Display.getDisplay(this);
display.setCurrent(fruitList1);
try{
Thread.currentThread().sleep(3000);
} catch(Exception e) {}
display.setCurrent(fruitList2);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Listing 2. Using Lists
List elements can be modified after the list has been created. You can modify
individual elements by changing their text, text font, or image part, using the
list index (starting at 0). You can delete elements using delete(int
index) or deleteAll(). Any changes take effect immediately,
even if the list is the current UI element being shown to the user.