Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with SELECT_COMMAND in List

 
Pat Nicholls
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having trouble getting List to work with type IMPLICIT. The code below demonstrates the problem When I select an option from the first list, everything works as expected. It is when I select an option from the second list nothing happens. I am setting a commandListener for each list and checking for the SELECT_COMMAND from each List. The SELECT_COMMAND is always from List 1 no matter which list you are in.
Many thanks for your help.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Untitled1 extends MIDlet implements CommandListener {
private Display display;
private List List1;
private List List2;
private List List3;
public Untitled1() {
display = Display.getDisplay(this);
}
public void showList1(){
String[] list1options = { "List2", "List3"};
List1 = new List("List 1", List.IMPLICIT,list1options,null);
List1.setCommandListener(this);
display.setCurrent(List1);
}
public void showList2(){
String[] list2options = { "List1", "List3"};
List2 = new List("List 2", List.IMPLICIT,list2options,null);
List2.setCommandListener(this);
display.setCurrent(List2);
}
public void showList3(){
String[] list3options = { "List1", "List2"};
List3 = new List("List 3", List.IMPLICIT,list3options,null);
List3.setCommandListener(this);
display.setCurrent(List3);
}
public void commandAction(Command c, Displayable d){
if (c == List1.SELECT_COMMAND){
int i = List1.getSelectedIndex();
if (i == 0){
System.out.println("List 1 SELECT_COMMAND");
showList2();
}
else {
System.out.println("List 1 SELECT_COMMAND");
showList3();
}
}
else if (c == List2.SELECT_COMMAND){
int i = List2.getSelectedIndex();
if (i == 0){
System.out.println("List 2 SELECT_COMMAND");
showList1();
}
else {
System.out.println("List 2 SELECT_COMMAND");
showList3();
}
}
else if (c == List3.SELECT_COMMAND){
int i = List3.getSelectedIndex();
if (i == 0){
System.out.println("List 3 SELECT_COMMAND");
showList1();
}
else {
System.out.println("List 3 SELECT_COMMAND");
showList2();
}
}
}
public void startApp() {
showList1();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
 
Richard Taylor
Author
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost spot on...
The bit that doesn't work is the test against SELECT_COMMAND, because this returns the same value for any List. In other words use SELECT_COMMAND when you just want to know that a List generated an action, and nothing more.
If you want to find which List, then compare the Displayable to each List, i.e.:
public void commandAction(Command c, Displayable d){
//if (c == List1.SELECT_COMMAND)
if (d == List1){
etc...
 
Pat Nicholls
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot - thats solved my problem. I just check which Displayable generated the action and then use getSelectedIndex() to find out what was selected.
Thanks again
Pat
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!