Hi!

I've programmed a MIDlet that can organize a tournament (save player datas, show rankings...). Right now it can only manage one tournament at a time but now I'd like to be able to manage two simultaneous. I designed the midlet like the famous "Smarticket"-Midlet: a "wrapper" midlet class and a controller class that handles the events. For those of you who dont know the Smartticket example here is my subclass of MIDlet:


public class MagicTOMIDlet extends javax.microedition.midlet.MIDlet {
private TOUIController controller;

public void startApp() {
new Thread() {
public void run() {
controller = new TOUIController(MagicTOMIDlet.this);
controller.init(); // setCurrents() to MainMenu - the entry point of the program
}
}.start();
}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}
}


The problem is: How can I create two midlets so that I can switch between the 2 tournaments to enter for example new Data (resuts...)? The tournaments don't affect each other, they just have to run simultaneously. My phone is a Nokia 3510i and it can't just run 2 copies of the MIDlet.

I had two solutions in mind but both didn't work:

1.) Declare 2 controller classes controller1 and controller2 and a switch Command, that can switch between both. After starting the midlet ask the user if he wants to organize one or two tournaments.

Here is the code for the MIDlet subclass:

public class MagicTOMIDlet extends MIDlet implements CommandListener {
public TOUIController controller1, controller2;
private List list;
private Command okCommand;
private Command exitCommand;
private Display display;
private Thread t1, t2;
private MagicTOMIDlet midlet;

public void startApp() {
display = Display.getDisplay(this);

list = new List("Choose # of Drafts", List.IMPLICIT);
okCommand = new Command("OK", Command.OK, 1);
exitCommand = new Command("Exit", Command.EXIT, 5);

list.append("1 Draft", null);
list.append("2 Drafts", null);
list.addCommand(okCommand);
list.addCommand(exitCommand);
list.setCommandListener(this);

display.setCurrent(list);
}

public void commandAction(Command command, Displayable displayable) {
if ((command == okCommand) || (command == List.SELECT_COMMAND)) {
String label = list.getString(list.getSelectedIndex());
if (label.equals("1 Draft")) {
t1 = new Thread() {
public void run() {
midlet.controller1 = new TOUIController(midlet, TOUIController.DRAFT_SINGLE);
midlet.controller1.init();
}
};
t1.start();
} else if (label.equals("2 Drafts")) {
t1 = new Thread() {
public void run() {
midlet.controller1 = new TOUIController(midlet, TOUIController.DRAFT_1);
midlet.controller1.init();
}
};
t2 = new Thread() {
public void run() {
midlet.controller2 = new TOUIController(midlet, TOUIController.DRAFT_2);
midlet.controller2.init();
}
};
t2.start();
t1.start();
}
} else if (command == exitCommand) {
destroyApp(true);
}
}

And here parts of the controller class:

public class TOUIController implements CommandListener {

// ...

public TOUIController(MagicTOMIDlet midlet, byte id) {
this.midlet = midlet;
this.draftId = id; // DRAFT_1 = 1, DRAFT_2 = 2, DRAFT_SINGLE = 0

if (id == DRAFT_SINGLE) {
singleDraft = true;
} else if (id == DRAFT_1) {
otherDraftId = DRAFT_2;
singleDraft = false;
} else if (id == DRAFT_2) {
otherDraftId = DRAFT_1;
singleDraft = false;
}
}
public void commandAction(Command command, Displayable displayable) {
//...

else if (command == switchCommand) {
if (draftId == DRAFT_1) {
setCurrent(midlet.controller2.display.getCurrent());
} else if (draftId == DRAFT_2) {
setCurrent(midlet.controller1.display.getCurrent());
}
}
}

Somehow it doesn't work. If I try to Switch I land in the MainMenu of the same Tournament (here called: draft).
My 2nd solution would be to declare two midlets in MagicTOMIDlet and to pass both midlets to the controller class:

public class MagicTOMIDlet extends MIDlet implements CommandListener {
public TOUIController controller;
private List list;
private Command okCommand;
private Command exitCommand;
private Display display;
private Thread t1, t2;
public MagicTOMIDlet midlet1;
public MagicTOMIDlet midlet2;

public MagicTOMIDlet() {
midlet1 = this;
midlet2 = this;
}

public void startApp() {
// as above
}

public void commandAction(Command command, Displayable displayable) {
if ((command == okCommand) || (command == List.SELECT_COMMAND)) {
String label = list.getString(list.getSelectedIndex());
if (label.equals("1 Draft")) {
t1 = new Thread() {
public void run() {
midlet1.controller = new TOUIController(midlet1, TOUIController.DRAFT_SINGLE);
midlet1.controller.init();
}
};
t1.start();
} else if (label.equals("2 Drafts")) {
t1 = new Thread() {
public void run() {
midlet1.controller = new TOUIController(midlet1, midlet2, TOUIController.DRAFT_1);
midlet1.controller.init();
}
};
t2 = new Thread() {
public void run() {
midlet2.controller = new TOUIController(midlet2, midlet1, TOUIController.DRAFT_2);
midlet2.controller.init();
}
};
t2.start();
t1.start();
}
}
// ...
}

public class TOUIController implements CommandListener {

public TOUIController(MagicTOMIDlet midlet, MagicTOMIDlet otherMidlet, byte id) {
this.midlet = midlet;
this.otherMidlet = otherMidlet;
this.draftId = id;

if (id == DRAFT_SINGLE) {
singleDraft = true;
} else if (id == DRAFT_1) {
otherDraftId = DRAFT_2;
singleDraft = false;
} else if (id == DRAFT_2) {
otherDraftId = DRAFT_1;
singleDraft = false;
}
}

public void commandAction(Command command, Displayable displayable) {
// ...

} else if (command == switchCommand) {
setCurrent(otherMidlet.controller.display.getCurrent());
}
}
}
}

It same the same wrong behaviour as my first solution.
I'm tankful for any advise or solution you might give me!

Marko