List Demo : List « J2ME « Java






List Demo

List Demo

/* License
 * 
 * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  
 *  * Redistribution of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 * 
 *  * Redistribution in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *  
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *  
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility. 
 */

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

public class ListDemoMIDlet extends MIDlet {

    private Display              display;
    private int                  mode = List.IMPLICIT;

    private Command exitCommand = new Command( "Exit",
                                   Command.SCREEN, 2 );
    private Command selectCommand = new Command( "Select",
                                       Command.OK, 1 );
    private Command nextCommand = new Command( "Next",
                                   Command.SCREEN, 2 );
    public ListDemoMIDlet(){
    }

    protected void destroyApp( boolean unconditional )
                   throws MIDletStateChangeException {
        exitMIDlet();
    }

    protected void pauseApp(){
    }

    protected void startApp() throws
    MIDletStateChangeException {
        if( display == null ){ // first time called...
            initMIDlet();
        }
    }

    private void initMIDlet(){
        display = Display.getDisplay( this );
        display.setCurrent( new SampleList( mode ) );
    }

    public void exitMIDlet(){
        notifyDestroyed();
    }

    public static final String[] items = {
        "First", "Second", "Third", "Fourth"
    };


    class SampleList extends List implements
                           CommandListener {

        private int mode;

        SampleList( int mode ){
            super( "", mode, items, null );
            addCommand( exitCommand );
            addCommand( selectCommand );
            addCommand( nextCommand );
            setCommandListener( this );

            switch( mode ){
                case IMPLICIT:
                    setTitle( "Implicit" );
                    break;
                case EXCLUSIVE:
                    setTitle( "Exclusive" );
                    break;
                case MULTIPLE:
                    setTitle( "Multiple" );
                    break;
            }

            this.mode = mode;
        }

        public void commandAction( Command c,
                             Displayable d ){
            if( c == exitCommand ){
                exitMIDlet();
            } else if( c == selectCommand ){
                showSelection( false );
            } else if( c == SELECT_COMMAND ){
                showSelection( true );
            } else if( c == nextCommand ){
                if( mode == List.IMPLICIT ){
                    mode = List.EXCLUSIVE;
                } else if( mode == List.EXCLUSIVE ){
                    mode = List.MULTIPLE;
                } else {
                    mode = List.IMPLICIT;
                }

                display.setCurrent( new SampleList(
                                             mode ) );
            }
        }

        private void showSelection( boolean implicit ){
            Alert alert = new Alert(
                       implicit ? "Implicit Selection"
                               : "Explicit Selection" );
            StringBuffer buf = new StringBuffer();

            if( mode == MULTIPLE ){
                boolean[] selected = new boolean[ size() ];
                getSelectedFlags( selected );

                for( int i = 0; i < selected.length; ++i ){
                    if( selected[i] ){
                        if( buf.length() == 0 ){
                            buf.append(
                             "You selected: " );
                        } else {
                            buf.append( ", " );
                        }

                        buf.append( getString( i ) );
                    }
                }

                if( buf.length() == 0 ){
                    buf.append( "No items are selected." );
                }
            } else {
                buf.append( "You selected " );
                buf.append( getString(
                       getSelectedIndex() ) );
            }

            alert.setString( buf.toString() );
            alert.setTimeout( Alert.FOREVER );

            display.setCurrent( alert,display.getCurrent() );
        }
    }
}


           
       








Related examples in the same category

1.List ImplicitList Implicit
2.List CheckBoxList CheckBox
3.List RadioButtonList RadioButton
4.GUI Test in MIDletGUI Test in MIDlet
5.Travel ListTravel List
6.Payment MIDletPayment MIDlet
7.multiple Choice Listmultiple Choice List
8.Implicit ListImplicit List
9.List Item MIDletList Item MIDlet