Java Reference
In-Depth Information
public String getString(int elementNum)
public Image getImage(int elementNum)
public void delete(int elementNum)
public void deleteAll()
The
deleteAll()
method removes every element from the
List
.
Finally, the
size()
method returns the number of elements in the
List
.
Although you usually give the MIDP implementation the responsibility of displaying your
List
, methods in
List
give you some control over the appearance of a
List
. The first method,
setFitPolicy()
, tells the
List
how it should handle elements whose text is wider than the screen.
The possible values (from the
Choice
interface) are the following:
•
TEXT_WRAP_ON
denotes that long elements will be wrapped to multiple lines.
•
TEXT_WRAP_OFF
denotes that long elements will be truncated at the edge of the screen.
•
TEXT_WRAP_DEFAULT
indicates that the implementation should use its default fit policy.
Another new method is
setFont()
, which allows you to specify the font that will be used for
a specific
List
element. (Fonts will be fully discussed in Chapter 13.) The current
Font
for an
element can be retrieved by calling
getFont()
. Calls to
setFitPolicy()
and
setFont()
only
serve as hints; it's up to the implementation to decide how to display the
List
and whether the
requested fit policy or font can be honored.
Working with List Selections
You can find out whether a particular element in a
List
is selected by supplying the element's
index to the following method:
public boolean isSelected(int index)
For
EXCLUSIVE
and
IMPLICIT
lists, the index of the single selected element is returned from
the following method:
public int getSelectedIndex()
If you call
getSelectedIndex()
on a
MULTIPLE
list, it will return
-1
.
To change the current selection programmatically, use
setSelectedIndex()
.
public void setSelectedIndex(int index, boolean selected)
Finally,
List
allows you to set or get the selection state en masse with the following methods.
The supplied arrays must have as many array elements as there are list elements.
public int getSelectedFlags(boolean[] selectedArray_return)
public void setSelectedFlags(boolean[] selectedArray)
An Example
The example in Listing 6-1 shows a simple MIDlet that could be part of a travel reservation
application. The user chooses what type of reservation to make. This example uses an
IMPLICIT
list, which is essentially a menu.
Search WWH ::
Custom Search