Monday, May 6, 2013

Get event click list item in J2ME

In midp a list is actually more like a menu with a selection of choices. You have to set a command on it, so that in your command action you can dispatch on this command, get the selection form the menu and set the next screen accordingly.

package com.pkm;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;

/**
 * @author User
 */
public class HelloMIDlet extends MIDlet implements javax.microedition.lcdui.CommandListener {
    private Display display;
    
    private Form form = new Form("Sign In Please");
    
    private Command submit = new Command("Submit", Command.SCREEN, 1);
    private Command exit = new Command("Exit", Command.EXIT, 1);
    private Command contactList = new Command("contactList", Command.OK, 1);
    private Command selection=new Command("Select", Command.ITEM, 1);
    
    List services;
    
    private TextField userName = new TextField("First Name:", "", 50, TextField.ANY);
    private TextField password = new TextField("Password", "", 30, TextField.PASSWORD);

    public HelloMIDlet() {
        display = Display.getDisplay(this);
        form.addCommand(submit);
        form.addCommand(exit);
        form.append(userName);
        form.append(password);
        form.setCommandListener(this);
    }

    public void startApp() {
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == submit) {
            validateUser(userName.getString(), password.getString());
        } else if (command == exit) {
            destroyApp(false);
            notifyDestroyed();
        } else if (command == contactList) {
            ContactList contactList = new ContactList("Contact List", this);
            display.setCurrent(contactList);
        } else if (command == selection) {
            int index = services.getSelectedIndex();
            Alert alert = new Alert("Your selection",
            "You chose " + services.getString(index) + ".",
            null, AlertType.INFO);
            Display.getDisplay(this).setCurrent(alert, services);
        }
    }
    
    public void validateUser(String name, String password) {
        if (name.equals("a") && password.equals("a")) {
            menu();
        } else {
            tryAgain();
        }
    }
    
    public void menu() {
        services = new List("Choose one", List.IMPLICIT);
        services.append("Check Mail", null);
        services.append("Compose", null);
        services.append("Addresses", null);
        services.append("Options", null);
        services.append("Sign Out", null);
        services.setSelectCommand(selection);
        services.addCommand(contactList);
        services.setCommandListener(this);
        
        display.setCurrent(services);
    }

    public void tryAgain() {
        Alert error = new Alert("Login Incorrect", "Please try again", null, AlertType.ERROR);
        error.setTimeout(Alert.FOREVER);
        userName.setString("");
        password.setString("");
        display.setCurrent(error, form);
    }

    void displayMainMIDlet() {
        menu();
    }
}

No comments:

Post a Comment