11. ConstrucIng
Lists
• When
you
create
a
list,
you
define
list's
– type
(EXCLUSIVE,
MULTIPLE,
IMPLICIT)
– Itle
– string
elements
and
images
(voluntary)
• API:
– public List(String title, int type):
– public List(String title, int type, String [] stringelements, Image
[] imageElements);
• Scrolling
is
automaIcally
available
• It
is
possibly
to
add
elements
to
the
list
with
append-
method.
12. List
Example
1
public class MyList extends MIDlet{
private List mList;
public MyList() {
mList = new List(”Wanna Buy?”, Choice.MULTIPLE);
mList.append(“Herring”, null);
mList.append(“Goose Liver”, null);
mList.append(“Jelly”, null);
}
public void startApp() {
Display.getDisplay(this).setCurrent(mList);
}
public void destroyApp(boolean unconditional) {}
public void pauseApp() {}
}
13. List
Example
2
public MyList() {
String [] elements =
{"hyytelöä", "kananmaksaa",
"ituja"};
mLista = new List("Haluatko
ostaa?", Choice.MULTIPLE,
elements, null);
}
14. Adding
and
Removing
List
elements
• It
is
possible
to
add
and
remove
list
elements
• The
elements
are
accessible
by
index
(0...n)
• Replacing
a
element:
set(...)
• Adding
element:
append(...)
• Adding
element
to
a
certain
place:
insert(...)
• Finding
element:
String getString(int n)
• Removing
element:
void delete(int n)
15. MIDP
2.0
List
• How
do
you
treat
the
elements,
if
they
don't
fit
into
mobile
device's
screen?
– setPolicy(...);
• Choice.TEXT_WRAP_ON,
Choice.TEXT_WRAP_OFF,
Choice.TEXT_WRAP_DEFAULT
• Changing
the
font:
– setFont(...)
16. List
SelecIon
• How
to
find
out
what
element
user
selected?
– public boolean isSelected(int i)!
– public int getSelectedIndex()!
– public void setSelectedIndex(int i, boolean s)!
• See
ListExample.java
17. Displayable
Class
Hierarchy
javax.microedi-on.lcdui
javax.microedi-on.lcdui.game
Displayable
Canvas
GameCanvas
Screen
Alert
List
Form
TextBox
18. Intro
to
Form
• Form
is
a
GUI-‐component
that
can
hold
other
GUI-‐
components
• These
othe
GUI-‐components
are
called
items.
• If
there
are
many
items,
the
form
will
provide
scrolling
mechanism.
• Constructors:
– public Form(String title)!
– public Form(String title, Item[] items)!
19. Handling
Items
• Adding
Items
to
the
Form:
– Form a = new Form(“otsikko”);!
– a.append(Item item);!
– a.append(String str);!
– a.append(Image image);!
• Every
item
has
an
index:
– a.set(int index, Item item)!
– a.insert(int index, Item item)!
– a.delete(int index)!
• Other
methods:
– int size();!
– Item get(int index)!
20. Simple
Example
mForm = new Form("Otsikko");
mForm.append("moi");
mForm.append("hei");
mForm.append("tere");
21. Using
Items
• There
are
many
items.
• javax.microedition.lcdui.Item:!
– ChoiceGroup!
– CustomItem // We will look this..!
– DateField!
– Gauge!
– ImageItem // .. and this later!
– Spacer!
– StringItem!
– TextField!
22. Item
Layout
• Every
Form
item
can
have
a
layout:
– LAYOUT_LEFT!
– LAYOUT_RIGHT!
– LAYOUT_CENTER!
– ...!
• See
API!
23. Using
Item
Layout
StringItem layoutLeft = new StringItem("Layout: ", "LEFT");
layoutLeft.setLayout(Item.LAYOUT_LEFT);
StringItem layoutCenter = new StringItem("Layout: ", "CENTER");
layoutCenter.setLayout(Item.LAYOUT_CENTER);
StringItem layoutRight = new StringItem("Layout: ", "RIGHT");
layoutRight.setLayout(Item.LAYOUT_RIGHT);
mForm.append(layoutLeft);
mForm.append(layoutCenter);
mForm.append(layoutRight);
24. StringItem
• Represents
a
simple
“Label”
Form form = new Form("Title");
StringItem st = new StringItem("Label: ";
"Value");
form.append(stringItem);
25. Item:
Spacer
• Spacer
provides
empty
space
in
the
form.
– Spacer empty = new Spacer(100,50); !
26. Item:
TextField
• TextField:
– public TextField
(String label,
String text, int
maxSize, int
constraints)!
• Constraints
(same
as
in
TextBox)
– ANY, NUMERIC,
DECIMAL,
PHONENUMBER,
EMAILADDR, URL!
– PASSWORD, SENSITIVE;
UNEDITABLE...!
27. Item:
DateField
• Date
and
Time
input.
– public DateField(String label, int mode)!
– public DateField(String label, int mode,
TimeZone timezone)!
• mode
– DATE, TIME, DATE_TIME!
• Get
and
Set-‐methods:
– public Date getDate()!
– public void setDate()!
29. Item:
Gauge
• Gauge
presents
integer
in
a
graphical
form.
– public Gauge(String label, boolean interactive,
int maxValue, int initialValue)
• By
using
parameter
interacIve
it
is
possible
to
define
can
the
user
modify
the
gauge.
• IniIalValue
can
also
be:
• CONTINUOUS_IDLE !
• INCREMENTAL_IDLE !
• CONTINUOUS_RUNNING !
• INCREMENTAL_UPDATING !
30. Gauge
Images
Gauge kake1 = new Gauge("Editoitava", true, 100, 0);
Gauge kake2 = new Gauge("Ei-editoitava", false, 100,
50);
Gauge kake3 = new Gauge("INCREMENTAL_UPDATING",
false, Gauge.INDEFINITE,
Gauge.INCREMENTAL_UPDATING);
Gauge kake6 = new Gauge("CONTINUOUS_RUNNING",
false, Gauge.INDEFINITE,
Gauge.CONTINUOUS_RUNNING);
31. Item:
ChoiceGroup
• Like
List
– public ChoiceGroup(String label,
int choiceType, String []
elements, Image[] imageElements)!
• Choicetype
– EXCLUSIVE!
– MULTIPLE!
– MIDP 2.0: POPUP!
32. ChoiceGroup
Images
String [] lista = {"suomi", "ruotsi"};
ChoiceGroup choicegroup = new ChoiceGroup("Valitse
maa", Choice.POPUP, lista, null);
33. Items
and
Event
Handling
• Instead
of
using
CommandListener
you
now
use
ItemStateListener.
• Set
the
event
source
to
form
and
the
listener
has
to
implement
ItemStateListener
interface.
• The
interface
has
only
one
method:
– itemStateChanged(Item item)!
• Example