MorkaLork Development

Interesting stuff I've picked up over the years...

Using a list

2009-06-18 17:22:58 | 590 views | list mobile cellular control

The List class


The list class allows you to create listobjects as a screen object. It inherits the Screen class and so it can be used as a displayable object.

There are three types of lists, IMPLICIT, EXLUSIVE and MULTIPLE. They look like this:


IMPLICIT:
Works similarly to buttons on a row. When selecting a list item a select command is sent directly.
An implicit list

EXCLUSIVE:
Works similarly to radio buttons. Only one item can be selected at a time.
An exclusive list

MULTIPLE:
Works similarly to check boxes. Multiple values can be selected.
A multiple list

This article will handle how to use the MULTIPLE list allowing the user of the MIDlet to select multiple values.


The basics


We will create a project and call it ListMIDlet which will inherit the MIDlet class.
We'll start of with the basic fields:


private Display display;
private Alert alert;

private Command cmdExit = new Command("Exit", Command.EXIT, 2);
private Command cmdSelect = new Command("Select", Command.OK, 1);


The Alert object will be explained later, but it is somewhat similar to the normal Alert box.
We can see that a List object is declared here, we will handle it later.

We'll create two commands; the exit command is a given, we want to be able to exit the MIDlet, and a select command to handle what se select in the list (this would not have been needed had we used the IMPLICIT list type).

There are three more declarations to be made that are directly involved in the List making of this MIDlet:


private List list;
private int listMode = List.EXCLUSIVE;
private final static String[] items = {"Red", "Blue", "Green", "Yellow", "White", "Black"};


We declare a List object to be used later. We also create an int, listMode, so to better show what kind of list we are using.
Since this is only an example, we also have a static constant list that holds our list items.


Implementing the MIDlet class


Now, as this is a midlet we will have to implement some abstract methods from MIDlet and one of them is startApp(){}


public void commandAction(Command c, Displayable d){}

public Display getDisplay(){
return Display.getDisplay(this);
}

public void startApp() {
if(display == null){
initMIDlet();
}
}

public void initMIDlet(){
display = Display.getDisplay(this);
list = new List("Colors: ", listMode, items, null);
list.addCommand(cmdExit);
list.addCommand(cmdSelect);
list.setCommandListener(this);
display.setCurrent(list);
}

[invDivClosed]
List

Syntax:
public List (String title, int listType)
public List (String title, int listType, String[] elements, Image[] images)

title is a string that is shown at the top of the list.
listType defines what kind of list it is, be it IMPLICIT, EXCLUSIVE or MULTIPLE.
elements are the items of the list.
images are images that can be used alone or with the list text items.
[/invDivClosed]

What we do in the method initMIDlet is that we instantiate the list object using the second constructor and so we set all the parameters at once. Since we will be displaying various colors the title should be Colors. The 'listmode' variable is the one we set earlier to List.MULTIPLE. 'items' was our string array containing the textual representation of our colors.

What we do now is that we add the commands we created earlier to this list since it is the list that will be handling the commands.

If we start the MIDlet it will now look like this:

A MIDlet with a list of type MULTIPLE


Adding commands


We, however, want something to happen when we press the commands, so we will add two commands to handle in the commandAction() method: cmdExit and cmdSelect.


public void commandAction(Command c, Displayable d){
if(c == cmdExit){
exitMIDlet();
}
else if (c == cmdSelect){
boolean[] selected = new boolean[list.size()];
list.getSelectedFlags(selected);
StringBuffer buffer = new StringBuffer();
for(int i = 0; i < selected.length; i++){
if(selected[i ]){
buffer.append(list.getString(i));
buffer.append("\n");
}
}

//Create an Alert object
try{
Image alertLogo = Image.createImage("/org/morkalork/listExamples/MorkAlert.png");
alert = new Alert("You selected: ", buffer.toString(), alertLogo, AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
}
catch (Exception e){
System.out.println("EXCEPTION: " + e.toString());
}

display.setCurrent(alert, display.getCurrent());
}
}

public void exitMIDlet(){
destroyApp(true);
notifyDestroyed();
}


The first command (cmdExit) you should understand quite easily since we used it before. It simpy exits the MIDlet for you.

The second command however is the interesting one. We start out with a boolean array that we call selected. This array will hold all the items in the list that the user has selected, so in the following line we use the .getSelectedFlags method in the list object and fill the array.

Now, the idea of this command is that we will show an Alert box where we list the selected items together with an icon (just for fun), so we will loop through the selected array and add the name of the color to a StringBuffer object (called buffer).
We also add a "\n" to every entry so that we get a new line for every item.

After having created the string list of the colors selected we create the Alert box. We will use an image that we stored in the project file (as you can see the address you might be able to figure out where to save the image).

We have to use try ... catch here since createImage throws exceptions.

After creating an icon (and you don't have to do this) we create the Alert object using its constructor.

[invDivClosed]
Alert

public Alert (java.lang.String title)
public Alert (java.lang.String title,String alertText, Image alertImage, AlertType alertType)


title is the title string, may be null
alertText is the string text of the Alert box, may be null
alertImage is the image contents of the Alert box, mayb be null
alertType is the type of the Alert box, may be null. (There are 5 kinds of AlertTypes: ALARM, CONFIRMATION, ERROR, INFO, WARNING)
[/invDivClosed]

We set the timeout to forever which means that the Alert box will stay visible until the user dismisses it.

We then apply it to the display.


If we run the application now this is what we will see when we've selected a couple of items.

The end result, with explanation

And now you have a working MIDlet with a pretty neat Alert box showing.


The full code




package org.morkalork.listExamples;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* Lets a user select colors from a list
*
* @author maffelu
*/
public class ListMIDlet extends MIDlet implements CommandListener{
private Display display;
private Alert alert;

private Command cmdExit = new Command("Exit", Command.EXIT, 2);
private Command cmdSelect = new Command("Select", Command.OK, 1);

private List list;
private int listMode = List.MULTIPLE;
private final static String[] items = {"Red", "Blue", "Green", "Yellow", "White", "Black"};

public void commandAction(Command c, Displayable d){
if(c == cmdExit){
exitMIDlet();
}
else if (c == cmdSelect){
boolean[] selected = new boolean[list.size()];
list.getSelectedFlags(selected);
StringBuffer buffer = new StringBuffer();
for(int i = 0; i < selected.length; i++){
if(selected[italic]){
buffer.append(list.getString(i));
buffer.append("\n");
}
}

//Create an Alert object
try{
Image alertLogo = Image.createImage("/org/morkalork/listExamples/MorkAlert.png");
alert = new Alert("You selected: ", buffer.toString(), alertLogo, AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
}
catch (Exception e){
System.out.println("EXCEPTION: " + e.toString());
}

display.setCurrent(alert, display.getCurrent());
}
}

public void exitMIDlet(){
destroyApp(true);
notifyDestroyed();
}

public void initMIDlet(){
display = Display.getDisplay(this);
list = new List("Colors: ", listMode, items, null);
list.addCommand(cmdExit);
list.addCommand(cmdSelect);
list.setCommandListener(this);
display.setCurrent(list);
}

public Display getDisplay(){
return Display.getDisplay(this);
}

public void startApp() {
if(display == null){
initMIDlet();
}
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}



Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.