Reputation: 147
EDIT: I believe I need help getting the selected element in the list I just managed
for it to display a new form but I'm having a lot of trouble finding code that works
with source 3.0.
I've been trying to make a application that allows a user to select a date then add
and remove events based on the selected date. So far I have created the first screen
which is a list of option for the user to choose from.
These options are:
The issues I'm having is I can't get my head around how to display new forms based on
the selected Item in the list. I found a small tutorial that allowed me to add a command
listener which shows the selected item but I'm having trouble figuring out how it gets the
item selected in the list and how I could create a new form based on the item selected?
Here's my code so far.
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class mainMidlet extends MIDlet implements CommandListener {
private Display display;
private List list = new List("Please Select a Option", List.IMPLICIT);
private Command select = new Command("Select", Command.SCREEN, 1);
private Form form;
Alert alert;
public mainMidlet() {
display = Display.getDisplay(this);
list.append("Select Date", null);
list.append("Add Events", null);
list.append("Remove Events", null);
list.append("Browse Events", null);
list.addCommand(select);
list.setCommandListener(this);
}
public void startApp() {
display.setCurrent(list);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == List.SELECT_COMMAND) {
String selection = list.getString(list.getSelectedIndex());
alert = new Alert("Option Selected", selection, null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
} else if (command == select) {
destroyApp(false);
notifyDestroyed();
}
}
}
Upvotes: 3
Views: 1159
Reputation: 17105
You can add several forms and switch between them
public void commandAction(Command command, Displayable displayable) {
if (displayable == list) {
if (command == List.SELECT_COMMAND) {
switch (list.getSelectedIndex()) {
case 0: // select date
display.setCurrent(someForm);
break;
case 1: //add events
display.setCurrent(someOtherForm);
break;
}
} else if (command == select) {
destroyApp(false);
notifyDestroyed();
}
}
if (displayable == someForm) {
//but it's better practice to make each form a different class implementing CommandListener and it's own commandAction. And leave the display public static in MIDlet class
//...
}
}
Upvotes: 4