creating commands at runtime
Does anyone have a good reference on how to generate commands at runtime? I'm working with String arrays of various sizes and I would like to create a new screen command for each item in the array.
MyCommand = new Command("MyCommand", Command.SCREEN, 1); So my users can click the menu button and look at the list of items from the array. Here's is what i have sofar for this:
String MyArray[][]
for (int n = 0; n < MyArray.length ; n++)
{
String nc ="Command" + n;
nc =new Command(MyArray[n][0], Command.SCREEN, 1);
timesMenu.addCommand(nc);
}
which is probably not even close but maybe this gives someone a better idea of what im trying to do. For the first item in the array, string nc would equal "CommandZero"(given that the value of MyArray[1][0] equals "Zero"). Then i would try to create a command called "CommandZero" and its display name would be "Zero", then it would be added.
Thank you!
[1152 byte] By [
Russixa] at [2007-11-27 9:48:53]

# 1
Hey, i figured out, but here is some info for anyone looking to do the same thing
public void pickTimes()
{
timesMenu = new List("Select Time", Choice.IMPLICIT, routes[0], null);
for (int n = 0; n < routes.length ; n++)
{
System.out.println(n + "Array Section");
timesMenu.addCommand(new Command(routes[n][0], routes[n][0], Command.SCREEN, 1));
}
mDisplay.setCurrent(timesMenu);
}
# 2
If I understand you correctly you want something like this:
public List get_ListOfItems() {
if (ListOfItems == null) {
// Insert pre-init code here
ListOfItems = new List("Please choose item:", Choice.IMPLICIT, new String[0], new Image[0]);
ListOfItems.addCommand(get_exitCommand1());
ListOfItems.addCommand(get_okCommand2());
ListOfItems.setCommandListener(this);
ListOfItems.setSelectedFlags(new boolean[0]);
//Get list of items available to this user and show them to the screen
try{
for (int i = 0; i < services.length; i++){
this.ListOfItems.append(items[i].itemName, null); //Append item name to the GUI list
}
}catch(Exception e){
e.printStackTrace();
}
}
return ListOfItems;
}
And
if (displayable == ListOfItems) {
if (command == ListOfItems.SELECT_COMMAND) {
int selectedIndex = get_ListOfItems().getSelectedIndex();
System.out.println("Selected List index is: " + selectedIndex);
//Do you stuff here with the selected object, like call a wait screen, etc
}
}
The code above basically creates a list, populates it with objects from Array and gives you ability to select those objects.
Then based on the selection you can have a separate class do some work and open up the next screen based on that.
Most of the code above you can generate with something like Netbeans in flow/screen design phase of the midlet.