Java ME handling multiple screens
I am creating an application in which when the application starts, the user has an option to add a file to the application which is residing on memory card.There is a button named add new
; when the user clicks on add new
a form will appear,in which user will enter the file name and can add the file but the command for adding new file is not working can any one suggest me whats going wrong there?
My code snippet is
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.file.*;
import javax.microedition.io.Connector;
import java.io.IOException;
import java.util.*;
public class HelloMIDlet
extends MIDlet
implements CommandListener {
private List list;
private Alert alert;
private Display display;
private Form form;
private TextField fname,fpath;
private Command select,remove,add,fadd,back,exit;
public HelloMIDlet() {
form=new Form("add new:");
fname=new TextField("enter File Name","",40,TextField.ANY);
fpath=new TextField("enter File path","file:///SDCard/",50,TextField.ANY);
list = new List("Welcome", List.IMPLICIT);
remove=new Command("Remove Selected",Command.SCREEN,2);
exit=new Command("Exit",Command.EXIT,0);
select=new Command("Select",Command.SCREEN,1);
add=new Command("Add New",Command.SCREEN,2);
list.addCommand(exit);
list.addCommand(select);
list.addCommand(add);
list.addCommand(remove);
form.append(fname);
form.append(fpath);
list.setCommandListener(this);
}
public void startApp() {
开发者_开发知识库display=Display.getDisplay(this);
display.setCurrent(list);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) {
if(c==exit)
{
notifyDestroyed();
}
else if(c==add)
{
// display.setCurrent(form);
addfile();
}
else if(c==remove)
{
}
else if(c==back)
{
display.setCurrent(list);
}
else if(c==fadd)
{
alert=new Alert("Open the file:","Would you like to open the current file??",null,null);
alert.setTimeout(Alert.FOREVER);
}
else if(c==list.SELECT_COMMAND)
{
}
}
public void addfile()
{fadd=new Command("add File",Command.SCREEN,0);
back=new Command("Back",Command.BACK,1);
form.addCommand(fadd);
form.addCommand(back);
form.setCommandListener(this);
display.setCurrent(form);
}
}
When you select on "add File", the command is triggered and handled by commandAction
, specifically, this piece of code:
else if(c==fadd)
{
alert=new Alert("Open the file:","Would you like to open the current file??",null,null);
alert.setTimeout(Alert.FOREVER);
// You were creating a Alert instance, but not showing it, this line below is one you
// were missing
display.setCurrent(alert, list);
}
You can either use setCurrent(Alert alert, Displayable nextDisplayable)
or setCurrent(Displayable nextDisplayable)
, depending on what you need.
精彩评论