Help on List/Sublist menu....

Hi guys,

Need your help with the code below...I am trying to modify the code below that would appear like this:

Language-> English or Chinese -->display to console

SMS->Options>display to console

Call->Options-->display to console

in the commented portion below when I try to use switch (((List)d).getSelectedIndex()) {

case 0:

setEnglish();

break;

case 1:

setChinese();

break;

}

it always go to setEnglish() method....Please help me with this cause I am

still new in java and j2me..

Thanks in advance,

Psyeu

/*

* %W% %E%

*

* Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved.

* PROPRIETARY/CONFIDENTIAL

* Use is subject to license terms

*/

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

import java.lang.*;

/**

* The MIDlet demonstrates the different types of list supported by MIDP-2.0:

*EXCLUSIVE - a choice having exactly one element selected at time.

*IMPLICIT - a choice in which the currently focused element is

*selected when a Command is initiated.

*MULTIPLE - a choice that can have arbitrary number of elements

*selected at a time.

*

* @version 2.0

*/

public class ListDemo

extends MIDlet

implements CommandListener {

private final static Command CMD_EXIT =

new Command("Exit", Command.EXIT, 1);

private final static Command CMD_BACK =

new Command("Back", Command.BACK, 1);

private Display display;

private List mainList;

private List languageList;

private List smsList;

private List callList;

Locale locale = new Locale();

String location = System.getProperty( "microedition.locale" ) ;

// private boolean langSel=false;

private boolean firstTime;

public ListDemo() {

display = Display.getDisplay(this);

// these are the strings for the choices.

//String[] stringArray = {

//"English",

//"Chinese",

//

//};

// the string elements will have no images

Image[] imageArray = null;

/*

* create samples of the supported types:

*"Exclusive", "Implicit" and "Multiple"

*/

String [] strArray={"English","Chinese"};

languageList = new List("Language Options", Choice.IMPLICIT, strArray,

imageArray);

languageList.addCommand(CMD_BACK);

languageList.addCommand(CMD_EXIT);

languageList.setCommandListener(this);

String [] strSMS = {"Friends","Family"};

smsList = new List("SMS", Choice.IMPLICIT, strSMS, imageArray);

smsList.addCommand(CMD_BACK);

smsList.addCommand(CMD_EXIT);

smsList.setCommandListener(this);

String [] strCont = {"Add","Edit","Delete"};

callList = new List("Call", Choice.IMPLICIT, strCont,

imageArray);

callList.addCommand(CMD_BACK);

callList.addCommand(CMD_EXIT);

callList.setCommandListener(this);

firstTime = true;

}

protected void startApp() {

if (firstTime) {

// these are the images and strings for the choices.

Image[] imageArray = null;

try {

// load the duke image to place in the image array

Image icon = Image.createImage("/midp/uidemo/Icon.png");

// these are the images and strings for the choices.

imageArray = new Image[] {

icon,

icon,

icon

};

} catch (java.io.IOException err) {

// ignore the image loading failure the application can recover.

}

String[] stringArray = {

"Language Options",

"SMS",

"Call"

};

mainList = new List("Language Demo", Choice.IMPLICIT, stringArray,

imageArray);

mainList.addCommand(CMD_EXIT);

mainList.setCommandListener(this);

display.setCurrent(mainList);

firstTime = false;

}

}

protected void destroyApp(boolean unconditional) {

}

protected void pauseApp() {

}

public void commandAction(Command c, Displayable d) {

if (d.equals(mainList)) {

// in the main list

if (c == List.SELECT_COMMAND) {

if (d.equals(mainList)) {

switch (((List)d).getSelectedIndex()) {

case 0:

display.setCurrent(languageList);

System.out.println("Language");

//choose between English and Chinese List

// //if English is selected go to method setEnglish();

//if Chinese is selected go to method setChinese();

break;

case 1:

display.setCurrent(smsList);

System.out.println("SMS");

break;

case 2:

display.setCurrent(callList);

System.out.println("Call");

break;

}

}

}

}else {

// in one of the sub-lists

if (c == CMD_BACK) {

display.setCurrent(mainList);

}

}

if (c == CMD_EXIT) {

destroyApp(false);

notifyDestroyed();

}

}

private void setChinese() {

// TODO Auto-generated method stub

System.out.println("Chinese");

}

private void setEnglish() {

// TODO Auto-generated method stub

System.out.println("English");

}

}

[5321 byte] By [psyeua] at [2007-11-27 8:21:38]
# 1

This is what u should do to make it work :

/*

* %W% %E%

*

* Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved.

* PROPRIETARY/CONFIDENTIAL

* Use is subject to license terms

*/

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

import java.lang.*;

/**

* The MIDlet demonstrates the different types of list supported by MIDP-2.0:

* EXCLUSIVE - a choice having exactly one element selected at time.

* IMPLICIT - a choice in which the currently focused element is

* selected when a Command is initiated.

* MULTIPLE - a choice that can have arbitrary number of elements

* selected at a time.

*

* @version 2.0

*/

public class ListDemo

extends MIDlet

implements CommandListener {

private final static Command CMD_EXIT =

new Command ("Exit", Command.EXIT, 1);

private final static Command CMD_BACK =

new Command ("Back", Command.BACK, 1);

private Display display;

private List mainList;

private List languageList;

private List smsList;

private List callList;

Locale locale = new Locale ();

String location = System.getProperty ( "microedition.locale" ) ;

// private boolean langSel=false;

private boolean firstTime;

public ListDemo () {

display = Display.getDisplay (this);

// these are the strings for the choices.

// String[] stringArray = {

// "English",

// "Chinese",

//

// };

// the string elements will have no images

Image[] imageArray = null;

/*

* create samples of the supported types:

* "Exclusive", "Implicit" and "Multiple"

*/

String [] strArray={"English","Chinese"};

languageList = new List ("Language Options", Choice.IMPLICIT, strArray,

imageArray);

languageList.addCommand (CMD_BACK);

languageList.addCommand (CMD_EXIT);

languageList.setCommandListener (this);

String [] strSMS = {"Friends","Family"};

smsList = new List ("SMS", Choice.IMPLICIT, strSMS, imageArray);

smsList.addCommand (CMD_BACK);

smsList.addCommand (CMD_EXIT);

smsList.setCommandListener (this);

String [] strCont = {"Add","Edit","Delete"};

callList = new List ("Call", Choice.IMPLICIT, strCont,

imageArray);

callList.addCommand (CMD_BACK);

callList.addCommand (CMD_EXIT);

callList.setCommandListener (this);

firstTime = true;

}

protected void startApp () {

if (firstTime) {

// these are the images and strings for the choices.

Image[] imageArray = null;

try {

// load the duke image to place in the image array

Image icon = Image.createImage ("/midp/uidemo/Icon.png");

// these are the images and strings for the choices.

imageArray = new Image[] {

icon,

icon,

icon

};

} catch (java.io.IOException err) {

// ignore the image loading failure the application can recover.

}

String[] stringArray = {

"Language Options",

"SMS",

"Call"

};

mainList = new List ("Language Demo", Choice.IMPLICIT, stringArray,

imageArray);

mainList.addCommand (CMD_EXIT);

mainList.setCommandListener (this);

display.setCurrent (mainList);

firstTime = false;

}

}

protected void destroyApp (boolean unconditional) {

}

protected void pauseApp () {

}

public void commandAction (Command c, Displayable d) {

if (d.equals (mainList)) {

// in the main list

if (c == List.SELECT_COMMAND) {

if (d.equals (mainList)) {

switch (((List)d).getSelectedIndex ()) {

case 0:

display.setCurrent (languageList);

System.out.println ("Language");

break;

case 1:

display.setCurrent (smsList);

System.out.println ("SMS");

break;

case 2:

display.setCurrent (callList);

System.out.println ("Call");

break;

}

}

}

} else if (d==languageList) {

switch (((List)d).getSelectedIndex ()) {

case 0:

setEnglish ();

break;

case 1:

setChinese ();

break;

}

} else {

// in one of the sub-lists

if (c == CMD_BACK) {

display.setCurrent (mainList);

}

}

if (c == CMD_EXIT) {

destroyApp (false);

notifyDestroyed ();

}

}

private void setChinese () {

// TODO Auto-generated method stub

System.out.println ("Chinese");

}

private void setEnglish () {

// TODO Auto-generated method stub

System.out.println ("English");

}

}

find_suvro@SDNa at 2007-7-12 20:10:11 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2
SD,Thank you very much...Now I understand that I should have used another condition outside the previous switch statement...10 points for you...Regards,Steve
psyeua at 2007-7-12 20:10:11 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
Thanks 2 u 2 buddy 4 those dukes.. :-)
find_suvro@SDNa at 2007-7-12 20:10:11 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

Hi SD,

The English and Chinese word can now display to the console but I noticed that the back command didn't work anymore...When back command is selected it only displays "English" or "Chinese" word in the console depending which is highlighted between the two....

I try to edit the code but when the Back command is okay the English or Chinese word cannot be displayed in console...

Can you help me on this?

Thanks!

psyeua at 2007-7-12 20:10:11 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5
oops. delete the last else. thats it.
find_suvro@SDNa at 2007-7-12 20:10:11 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
Thanks for your response...I already got the solution..
psyeua at 2007-7-12 20:10:11 > top of Java-index,Java Mobility Forums,Java ME Technologies...