如何在J2me中显示其他类的List(LCDUI)?

时间:2011-03-20 09:23:31

标签: audio java-me lwuit

我有一个用于从手机内存浏览文件的类,它使用list来附加文件列表,我有另一个类作为mainInterface。

那么我如何将该列表类调用到我的mainInterface中?

这是文件浏览器类

public class FileBrowser implements CommandListener {

    private String currDirName;
    protected String fileName;
    private Command open = new Command("Open", Command.ITEM, 1);
    private Command back = new Command("Back", Command.BACK, 2);
    private Command exit = new Command("Exit", Command.EXIT, 3);

    private final static String UP_DIR = "...";
    private final static String MEGA_ROOT = "/";
    private final static String SEP_STR = "/";
    private final static char SEP = '/';

    public Display d;

    public FileBrowser(){

        currDirName = MEGA_ROOT;
        fileName = null;
    }

    public String getFileName(){
        return fileName;
    }

    public void startBrowsing(){
        boolean isAPIAvailable = false;

        if(System.getProperty("microedition.io.FileConnection.version") != null){
            isAPIAvailable = true;
            try{
                showCurrDir();



            }catch(SecurityException se){}
            catch(Exception e){}
            }
     else{ }
    }
    public void showCurrDir(){
        Enumeration e;
        FileConnection currDir = null;
        List browser;
        try{
            if(MEGA_ROOT.equals(currDirName)){
                e = FileSystemRegistry.listRoots();
                browser = new List(currDirName, List.IMPLICIT);

            }
            else{
                currDir = (FileConnection)Connector.open("file://localhost/" + currDirName);
                e = currDir.list();
                browser = new List(currDirName, List.IMPLICIT);
                browser.append(UP_DIR, null);
            }
            while(e.hasMoreElements()){
                fileName = (String)e.nextElement();
                if(fileName.charAt(fileName.length()-1) == SEP){
                    browser.append(fileName, null);

                } else{
                    browser.append(fileName, null);
                }
            }
            browser.setSelectCommand(open);
            browser.addCommand(exit);
            browser.setCommandListener(this);
            if(currDir != null){
                currDir.close();

            }

        } catch(IOException ioe){}

    }
    public void commandAction(Command c, Displayable d){
        if(c == open){
            List curr = (List)d;
            final String currFile = curr.getString(curr.getSelectedIndex());
        new Thread(new Runnable()
        {
            public void run()
        {
            if (currFile.endsWith(SEP_STR) ||
                currFile.equals(UP_DIR)) {
                traverseDirectory(currFile);
            }
            else {
                openFile(currFile);
            }
        }
      }).start();
        }
        else if (c == back)
        {
            showCurrDir();
        }

    }
    public void traverseDirectory(String fileName){
        if(currDirName.equals(MEGA_ROOT)){
            if(fileName.equals(UP_DIR)){
                return;
            }
            currDirName = fileName;
        } else if(fileName.equals(UP_DIR)){
            int i = currDirName.lastIndexOf(SEP, currDirName.length()-2);
            if(i != -1){
                currDirName = currDirName.substring(0, i+1);
            }
            else{
                currDirName = MEGA_ROOT;
            }
        }
        showCurrDir();
    }
    public void openFile(String fileName){
        try{
            FileConnection fc = (FileConnection) Connector.open("file://localhost/" + currDirName + fileName);
            if(!fc.exists()){
                throw new IOException("File does not exists");
            }
            InputStream fileIS = fc.openDataInputStream();
            byte[] b = new byte[1024];

            int length = fileIS.read(b, 0, 1024);
            fileIS.close();
            fc.close();
            TextBox tb = new TextBox("View File: " + fileName, null, 1024, TextField.ANY | TextField.UNEDITABLE);
            tb.addCommand(back);
            tb.addCommand(exit);
            tb.setCommandListener(this);

            if (length > 0)
            {
                tb.setString(new String(b, 0, length));
            }
        } catch(Exception e){}

    }

}

这是mainInterface类的片段中的函数

public void actionPerformed(ActionEvent ae){
       if(ae.getSource() == open){
              start the filebrowsing screen
       }
}

1 个答案:

答案 0 :(得分:0)

您需要将其移植到LWUIT才能正常工作。 1.4版本的LWUITDemo在树部分包含了一个文件树演示。这也是LWUIT聊天演示的一部分。