dúvida básica em J2ME

Pessoal,

 Estou iniciando em J2ME e fiz um programa básico para mostrar um List, escolha um Element e mostrar o valor escolhido, entretanto, depois que escolho o primeiro valor, ele não se modifica mais. Alguém poderia me ajudar com o que está acontecendo?

Atenciosamente,
Petrus Bastos.

[code]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package hello;

import javax.microedition.midlet.;
import javax.microedition.lcdui.
;
import org.netbeans.microedition.lcdui.SplashScreen;

/**

  • @author Petrus Bastos
    */
    public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //
    private Command exitCommand;
    private Command itemCommand;
    private Command exitCommand1;
    private SplashScreen splashScreen;
    private List list;
    private Alert alert;
    private Image image1;
    private Font font1;
    //

    /**

    • The HelloMIDlet constructor.
      */
      public HelloMIDlet() {
      }

    //
    //

    //
    /**

    • Initilizes the application.

    • It is called only once when the MIDlet is started. The method is called before the startMIDlet method.
      */
      private void initialize() {
      // write pre-initialize user code here

      // write post-initialize user code here
      }
      //

    //
    /**

    • Performs an action assigned to the Mobile Device - MIDlet Started point.
      */
      public void startMIDlet() {
      // write pre-action user code here
      switchDisplayable(null, getSplashScreen());
      // write post-action user code here
      }
      //

    //
    /**

    • Performs an action assigned to the Mobile Device - MIDlet Resumed point.
      */
      public void resumeMIDlet() {
      // write pre-action user code here

      // write post-action user code here
      }
      //

    //
    /**

    • Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable.
    • @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately
    • @param nextDisplayable the Displayable to be set
      */
      public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
      // write pre-switch user code here
      Display display = getDisplay();
      if (alert == null) {
      display.setCurrent(nextDisplayable);
      } else {
      display.setCurrent(alert, nextDisplayable);
      }
      // write post-switch user code here
      }
      //

    //
    /**

    • Called by a system to indicated that a command has been invoked on a particular displayable.
    • @param command the Command that was invoked
    • @param displayable the Displayable where the command was invoked
      */
      public void commandAction(Command command, Displayable displayable) {
      // write pre-action user code here
      if (displayable == list) {
      if (command == List.SELECT_COMMAND) {
      // write pre-action user code here
      listAction();
      // write post-action user code here
      } else if (command == exitCommand1) {
      // write pre-action user code here
      exitMIDlet();
      // write post-action user code here
      } else if (command == itemCommand) {
      // write pre-action user code here
      switchDisplayable(getAlert(), getList());
      // write post-action user code here
      }
      } else if (displayable == splashScreen) {
      if (command == SplashScreen.DISMISS_COMMAND) {
      // write pre-action user code here
      switchDisplayable(null, getList());
      // write post-action user code here
      }
      }
      // write post-action user code here
      }
      //

    //
    /**

    • Returns an initiliazed instance of exitCommand component.
    • @return the initialized component instance
      */
      public Command getExitCommand() {
      if (exitCommand == null) {
      // write pre-init user code here
      exitCommand = new Command(“Exit”, Command.EXIT, 0);
      // write post-init user code here
      }
      return exitCommand;
      }
      //

    //
    /**

    • Returns an initiliazed instance of splashScreen component.
    • @return the initialized component instance
      */
      public SplashScreen getSplashScreen() {
      if (splashScreen == null) {
      // write pre-init user code here
      splashScreen = new SplashScreen(getDisplay());
      splashScreen.setTitle(“splashScreen”);
      splashScreen.setCommandListener(this);
      splashScreen.setFullScreenMode(true);
      splashScreen.setImage(getImage1());
      splashScreen.setText(“Aguarde…”);
      splashScreen.setTextFont(getFont1());
      splashScreen.setTimeout(2000);
      // write post-init user code here
      }
      return splashScreen;
      }
      //

    //
    /**

    • Returns an initiliazed instance of image1 component.
    • @return the initialized component instance
      */
      public Image getImage1() {
      if (image1 == null) {
      // write pre-init user code here
      try {
      image1 = Image.createImage("/logo.gif");
      } catch (java.io.IOException e) {
      e.printStackTrace();
      }
      // write post-init user code here
      }
      return image1;
      }
      //

    //
    /**

    • Returns an initiliazed instance of itemCommand component.
    • @return the initialized component instance
      */
      public Command getItemCommand() {
      if (itemCommand == null) {
      // write pre-init user code here
      itemCommand = new Command(“Escolher”, Command.SCREEN, 0);
      // write post-init user code here
      }
      return itemCommand;
      }
      //

    //
    /**

    • Performs an action assigned to the selected list element in the list component.
      */
      public void listAction() {
      // enter pre-action user code here
      switch (getList().getSelectedIndex()) {
      case 0:
      // write pre-action user code here

           // write post-action user code here
           break;
       case 1:
           // write pre-action user code here
      
           // write post-action user code here
           break;
       case 2:
           // write pre-action user code here
      
           // write post-action user code here
           break;
       case 3:
           // write pre-action user code here
      
           // write post-action user code here
           break;
      

      }
      // enter post-action user code here
      }
      //

    //
    /**

    • Returns an initiliazed instance of font1 component.
    • @return the initialized component instance
      */
      public Font getFont1() {
      if (font1 == null) {
      // write pre-init user code here
      font1 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
      // write post-init user code here
      }
      return font1;
      }
      //

    //
    /**

    • Returns an initiliazed instance of exitCommand1 component.
    • @return the initialized component instance
      */
      public Command getExitCommand1() {
      if (exitCommand1 == null) {
      // write pre-init user code here
      exitCommand1 = new Command(“Sair”, Command.EXIT, 0);
      // write post-init user code here
      }
      return exitCommand1;
      }
      //

    //
    /**

    • Returns an initiliazed instance of alert component.
    • @return the initialized component instance
      */
      public Alert getAlert() {
      if (alert == null) {
      // write pre-init user code here
      alert = new Alert(“Alerta”, "Você escolheu: " + (list.getSelectedIndex() + 1), null, AlertType.INFO);
      alert.setTimeout(Alert.FOREVER);
      // write post-init user code here
      }
      return alert;
      }
      //

    //
    /**

    • Returns an initiliazed instance of list component.
    • @return the initialized component instance
      */
      public List getList() {
      if (list == null) {
      // write pre-init user code here
      list = new List(“Lista de Cobran\u00E7as”, Choice.EXCLUSIVE);
      list.append(“Cobran\u00E7a 1”, null);
      list.append(“Cobran\u00E7a 2”, null);
      list.append(“Cobran\u00E7a 3”, null);
      list.append(“Cobran\u00E7a 4”, null);
      list.addCommand(getItemCommand());
      list.addCommand(getExitCommand1());
      list.setCommandListener(this);
      list.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
      list.setSelectCommand(getItemCommand());
      list.setSelectedFlags(new boolean[] { false, false, false, false });
      // write post-init user code here
      }
      return list;
      }
      //

    /**

    • Returns a display instance.
    • @return the display instance.
      */
      public Display getDisplay() {
      return Display.getDisplay(this);
      }

    /**

    • Exits MIDlet.
      */
      public void exitMIDlet() {
      switchDisplayable(null, null);
      destroyApp(true);
      notifyDestroyed();
      }

    /**

    • Called when MIDlet is started.
    • Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
      */
      public void startApp() {
      if (midletPaused) {
      resumeMIDlet();
      } else {
      initialize();
      startMIDlet();
      }
      midletPaused = false;
      }

    /**

    • Called when MIDlet is paused.
      */
      public void pauseApp() {
      midletPaused = true;
      }

    /**

    • Called to signal the MIDlet to terminate.
    • @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
      */
      public void destroyApp(boolean unconditional) {
      }
      }
      [/code]

Petrus,

Tentei entender mais ou menos o que está acontecendo, mas o Netbeans faz uma bagunça no código né? rsrs :wink:

Bom, pelo que entendi, ele está caindo nesta linha:

if (command == itemCommand) {
// write pre-action user code here
switchDisplayable(getAlert(), getList());
// write post-action user code here
}

Assim, ele mostra o alert e depois o list… estranho…

E se vc simplificasse o programa?
Tenta fazer na mão… um programinha deste tipo deveria ser bem mais simples que isso…

[]s

ramonchiara,

 Pois é, realmente o netbeans faz esta bagunça toda, porque eu estou utilizando o mobility pack. Entretanto, mesmo assim eu queria saber o porque deste código específico não funcionar da forma que quero. Acho que seria uma forma bastante interessante de aprender, visto que teria que entender o que está acontecendo.
 Olhei o código exaustivamente agora pela manhã e descobri qual era o problema...
     public Alert getAlert() {  
         if (alert == null) {  
             // write pre-init user code here  
             alert = new Alert("Alerta", "Você escolheu: " + (list.getSelectedIndex() + 1), null, AlertType.INFO);  
             alert.setTimeout(Alert.FOREVER);  
         // write post-init user code here  
         }  
         return alert;      
     }  

Com este código o programa nunca iria funcionar do jeito que eu queria, pois este getAlert sempre retornava o mesmo objeto com o primeiro título escolhido. :stuck_out_tongue: Este tal de netbeans… :stuck_out_tongue: e o pior é que você não pode alterar o código que ele propõe… Baseado na descoberta do problema, fiz uma pequena mudança para alterar o text do alert toda vez que o list for selecionado.

Agardeço por sua ajuda,
Petrus Bastos.