Get String of List MULTIPLE

List Multiple
Hehe, maaf ya kalo judulnya agak hancur. Kali ini ingin berbagi tentang bagaimana kita bisa mengambil string pilihan dari list tipe MULTIPLE. List tipe MULTIPLE memungkinkan penggunanya memilih banyak item, dan kita akan mencoba menampilkan string pilihan yang dipilih pengguna. Berikut sintaksnya:

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;
import javax.wireless.messaging.Message;


/**
 * @author Dyah Fajar
 * April 13th, 2011
 */
public class Chapter4 extends MIDlet implements CommandListener{
    private Display display;
    private List list;
    private Command cmdTampil;
    private Command cmdExit;
    private Form frm;


    public Chapter4() {
        String arr[]={"Menu 1", "Menu 2", "Menu 3"};
        display = Display.getDisplay(this);
        cmdTampil = new Command("Tampilkan", Command.OK, 0);
        cmdExit = new Command("Exit", Command.EXIT, 0);
        frm = new Form("Tampilan");
        list = new List("Daftar Menu", List.MULTIPLE, arr, null);


        list.addCommand(cmdTampil);
        list.setCommandListener(this);


        frm.addCommand(cmdExit);
    }


    public void startApp() {
        display.setCurrent(list);
    }


    public void pauseApp() {
    }


    public void destroyApp(boolean unconditional) {
    }


    public void commandAction(Command c, Displayable d) {
        if (c ==cmdTampil){
            boolean choice[]= new boolean[list.size()];
            StringBuffer buf = new StringBuffer();
            list.getSelectedFlags(choice);
            for (int i= 0; i < choice.length; i++){
                if (choice[i]){
                    System.out.println("HERE:" + choice[i]);
                    buf.append(list.getString(i));
                    buf.append("\n");
                }
            }
            frm.append(buf.toString());
            display.setCurrent(frm);
        }
    }
}

2 comments:

  1. ea bermanfaat Bu...., nie kan cz free tes kmarin ya?...

    ReplyDelete
  2. hehe, soalnya banyak yang nannya juga, jadi mending di posting aja, biar ga jelasin berulang2..

    ReplyDelete

Terima kasih sudah membaca....^^