3

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:

  • Select Date
  • Add Events
  • Remove Events
  • Browse Events

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();
        }
      }
    }
gnat
  • 6,213
  • 108
  • 53
  • 73
Skeng
  • 147
  • 3
  • 11
  • did you consider posting a new question instead of editing this one (which seem to be answered, isn't it)? At SO answerers tend to dislike ["chameleon questions"](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions "related discussion at MSO") – gnat May 01 '12 at 19:23

1 Answers1

4

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
          //...
      }
  }
Nitesh Verma
  • 1,795
  • 4
  • 27
  • 46
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • Totally forgot about switch and case, thank you will give it a go now :D – Skeng May 01 '12 at 14:40
  • 1
    Should it be implementing in case of extending? BTW i have changed it. – Nitesh Verma Aug 30 '13 at 08:37
  • @NiteshVerma absolutely. Thanks. – Yaroslav Mytkalyk Aug 30 '13 at 09:22
  • Your welcome. Actually i was working around the same and just came across this post. Can you tell me how can achieve this, "make each form a different class", i created a new class, added the form object and the listener but how do i add it in the switch case. – Nitesh Verma Aug 30 '13 at 09:24
  • @NiteshVerma I am no longer J2ME developer and don't remember much. And I did not understand your problem. Under "making each form" different class I meant make class YourClass extends Form and implement CommandListener. So in CommandAction your Displayable is your Form. So each Form handles it's Commands in it's own listener. You don't need switch if there is no List. Switch is there to check what item in List had been clicked. – Yaroslav Mytkalyk Aug 30 '13 at 10:42
  • Thanks, your idea worked out. I was wandering over some silly mistakes. – Nitesh Verma Aug 30 '13 at 10:54