小さなアプリを作りたい。ユーザーとパスワードでログインし、選択するオプションを表示します。オプションを選択すると、それに応じてアラートが表示されます。しかし、リストのイベントクリック項目を取得できません。
コードは次のとおりです。
public class Midlet extends MIDlet implements CommandListener{
private Display display;
private SampleCavans myCanvas;
private TextField username;
private TextField password;
private Form form;
private Command cancel;
private Command login;
private Command mNextCommand;
private List service;
String[] stringElements = { "Check Mail", "Compose","Addresses","Options","Signout","Calculator"};
public Midlet() {
display=Display.getDisplay(this);
username=new TextField("Login ID", "", 10, TextField.ANY);
password=new TextField("Password", "", 10, TextField.PASSWORD);
cancel=new Command("Cancel", Command.CANCEL, 2);
login=new Command("Login", Command.OK, 2);
form=new Form("Sign in");
form.append(username);
form.append(password);
form.addCommand(cancel);
form.addCommand(login);
form.setCommandListener(this);//add event click for form
myCanvas=new SampleCavans();
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void menu()
{
//List service=new List("Choice one",Choice.EXCLUSIVE);
service=new List("Choice one",Choice.EXCLUSIVE,stringElements,null);
service.setCommandListener(this);
//service.addCommand(mNextCommand);
display.setCurrent(service);
}
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);
}
public void commandAction(Command c, Displayable d) {
String lable=c.getLabel();
if(lable.equals("Cancel"))
{
destroyApp(true);
}
else if(lable.equals("Login"))
{
validateUser(username.getString(),password.getString());
}
else if(c==List.SELECT_COMMAND)
{
int index = service.getSelectedIndex();
Alert alert = new Alert("Your selection",
"You chose " + service.getString(index) + ".",
null, AlertType.INFO);
Display.getDisplay(this).setCurrent(alert, service);
}
}
private void validateUser(String name, String pass) {
if(name.equals("12")&&pass.endsWith("12"))
{
menu();
}
else
tryAgain();
}