Senin, 30 Mei 2011

listDinamic pada Mobile

Dengan listDinamic kita bisa menambah atau menghapus sebuah item .

Inlah contoh scriptnya :


import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class listDinamis extends MIDlet implements CommandListener{
    Display display;
    List list;
    TextBox textbox;

    Command exitCommand = new Command("Exit", Command.EXIT, 1);
    Command addCommand = new Command("Add Item", Command.OK, 1);
    Command removeCommand = new Command("Remove Item", Command.OK, 1);
    Command okCommand = new Command("OK", Command.OK, 1);
    Command cancelCommand = new Command("Cancel", Command.EXIT, 1);

public listDinamis(){
    list = new List("JEDI: Dynamic List", List.IMPLICIT);
    list.addCommand(exitCommand);
    list.addCommand(addCommand);
    list.addCommand(removeCommand);
    list.setCommandListener(this);
    textbox = new TextBox("Add List Item", "", 64, TextField.ANY);
    textbox.addCommand(okCommand);
    textbox.addCommand(cancelCommand);
    textbox.setCommandListener(this);
}
    public void startApp()
    {
        if (display == null){
        display = Display.getDisplay(this);
        display.setCurrent(list);
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand){
            destroyApp(true);
            notifyDestroyed(); // Exit
        }
        if (c == addCommand){
            textbox.setString("");//texfield sesuai
            display.setCurrent(textbox);
        }
        if (c == removeCommand){
            int index = list.getSelectedIndex();
            if (index != -1){
            list.delete(index);
            } else {
            // no item selected
            }
        }
        if (c == okCommand){
            int index = list.getSelectedIndex();
            if (index != -1){
            list.insert(index, textbox.getString(), null);
        } else {
        // list might be empty
            list.append(textbox.getString(), null);
        }
            display.setCurrent(list);
        }
        if (c == cancelCommand){
        display.setCurrent(list);
            }
        }
}

Maka tampilannya akan seperti ini :


Tidak ada komentar:

Posting Komentar