2011-02-15 57 views
0

我正在创建一个应用程序,当应用程序启动时,用户可以选择将文件添加到驻留在存储卡上的应用程序中。有一个按钮名为add new;当用户点击add new时,会出现一个表单,其中用户将输入文件名并可以添加文件,但添加新文件的命令不起作用,任何人都可以告诉我发生了哪些错误?Java ME处理多个屏幕

我的代码段是

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); 
    } 
} 

回答

1

当您在“添加文件”选择,指令被触发,通过commandAction处理,具体而言,这段代码:

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); 
} 

你可以根据您的需要使用setCurrent(Alert alert, Displayable nextDisplayable)setCurrent(Displayable nextDisplayable)

+0

它基本上是一个附属应用程序,其中一个csv文件将存储在存储卡上,用户可以将该文件添加到应用程序,当他添加文件时,他可以打开它来编辑考勤。所以通过警报,用户可以选择打开该文件进行编辑。 – CuriousCase 2011-02-15 17:25:01