How to display List (LCDUI) from other class in J2me?
i have a class for browsing the file from phone memory, it use list to append the file list and i have another class as the mainInterface.
so how i can call that list class into my mainInterface?
here is the file browser class
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 = c开发者_如何学GourrDirName.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){}
}
}
and here is the function in the snippet of mainInterface class
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == open){
start the filebrowsing screen
}
}
You will need to port it to LWUIT for it to work properly. The 1.4 version of the LWUITDemo included a file tree demo in the tree section. This is also a part of the LWUIT chat demo.
精彩评论