⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jun01_ericg.txt

📁 TechTips j2me的常用技巧. 网络功能
💻 TXT
📖 第 1 页 / 共 3 页
字号:
            bout.reset();
        }
        catch( Exception e ){
        }
    }

    // Fill record store with some precanned names

    private void fillRecordStore(){
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream( bout );

        for( int i = 0; i < names.length; ++i ){
            addName( names[i][0], names[i][1], bout, dout );
        }
    }

    // Open record store, if empty then fill it

    private boolean openRecordStore(){
        try {
            if( rs != null ) closeRecordStore();

            rs = RecordStore.openRecordStore( "EnumDemo", true );
            if( rs.getNumRecords() == 0 ){
                fillRecordStore();
            }
            return true;
        }
        catch( RecordStoreException e ){
            return false;
        }
    }

    // Close record store

    private void closeRecordStore(){
        if( rs != null ){
            try {
                rs.closeRecordStore();
            }
            catch( RecordStoreException e ){
            }

            rs = null;
        }
    }

    // Move to and read in a record

    private boolean readRecord( int id, Record r ){
        boolean ok = false;

        r.firstName = null;
        r.lastName = null;

        if( rs != null ){
            try {
                rs.getRecord( id, data, 0 );
                r.firstName = din.readUTF();
                r.lastName = din.readUTF();
                din.reset();

                ok = true;
            }
            catch( Exception e ){
            }
        }

        return ok;
    }

    // Event handling

    public void commandAction( Command c, Displayable d ){
        if( c == exitCommand ){
            exitMIDlet();
        } else if( c == sortCommand ){
            display.setCurrent( sortOptionsScreen );
        } else if( d == sortOptionsScreen ){
            if( c == okCommand ){
                enumListScreen.resort();
            }
            
            display.setCurrent( enumListScreen );
        }
    }

    // Main screen -- a list of names

    class EnumList extends List
                   implements RecordComparator, RecordFilter {
        private int    sortBy;
        private int    filterBy;
        private String filterText;
        private Record r1 = new Record();
        private Record r2 = new Record();

        // Constructor

        EnumList(){
            super( "Enum Demo", IMPLICIT );
            addCommand( exitCommand );
            addCommand( sortCommand );
            setCommandListener( EnumDemo.this );
        }

        // Resort the data and refill the list

        public void resort(){
            sortBy = sortOptionsScreen.getSortType();
            filterBy = sortOptionsScreen.getFilterType();
            filterText = sortOptionsScreen.getFilterText();

            RecordComparator comparator = null;
            RecordFilter     filter = null;

            if( sortBy != 0 ){
                comparator = this;
            }

            if( filterBy != 0 ){
                filter = this;
            }

            deleteAll();

            try {
                RecordEnumeration enum = rs.enumerateRecords( 
                                     filter, comparator, false );
                Record            r = new Record();

                while( enum.hasNextElement() ){
                    int id = enum.nextRecordId();
                    if( readRecord( id, r ) ){
                        if( sortBy == SORT_LAST_FIRST ){
                            append( r.lastName + ", " + 
                                             r.firstName, null );
                        } else {
                            append( r.firstName + " " + 
                                              r.lastName, null );
                        }
                    }
                }

                enum.destroy();
            }
            catch( RecordStoreException e ){
            }
        }

        // Delete all items in the list

        private void deleteAll(){
            int n = size();
            while( n > 0 ){
                delete( --n );
            }
        }

        // The comparator

        public int compare( byte[] rec1, byte[] rec2 ){
            try {
                ByteArrayInputStream  bin = 
                                new ByteArrayInputStream( rec1 );
                DataInputStream       din = 
                                      new DataInputStream( bin );

                r1.firstName = din.readUTF();
                r1.lastName = din.readUTF();

                bin = new ByteArrayInputStream( rec2 );
                din = new DataInputStream( bin );

                r2.firstName = din.readUTF();
                r2.lastName = din.readUTF();

                if( sortBy == SORT_FIRST_LAST ){
                    int cmp = r1.firstName.compareTo( 
                                                  r2.firstName );
                 System.out.println( r1.firstName + 
                  " compares to " + r2.firstName + 
                                               " gives " + cmp );
                    if( cmp != 0 ) return ( 
                                  cmp < 0 ? PRECEDES : FOLLOWS );
                    cmp = r2.lastName.compareTo( r2.lastName );
                    if( cmp != 0 ) return ( 
                                  cmp < 0 ? PRECEDES : FOLLOWS );
                } else if( sortBy == SORT_LAST_FIRST ){
                    int cmp = r1.lastName.compareTo( 
                                                   r2.lastName );
                    if( cmp != 0 ) return ( 
                                  cmp < 0 ? PRECEDES : FOLLOWS );
                    cmp = r2.firstName.compareTo( r2.firstName );
                    if( cmp != 0 ) return ( 
                                  cmp < 0 ? PRECEDES : FOLLOWS );
                }
            }
            catch( Exception e ){
            }

            return EQUIVALENT;
        }

        // The filter

        public boolean matches( byte[] rec ){
            try {
                ByteArrayInputStream  bin = 
                                 new ByteArrayInputStream( rec );
                DataInputStream       din = 
                                      new DataInputStream( bin );

                r1.firstName = din.readUTF();
                r1.lastName = din.readUTF();

                if( filterBy == FILTER_STARTSWITH ){
                    return( r1.firstName.startsWith( filterText ) 
                            ||
                          r1.lastName.startsWith( filterText ) );
                } else if( filterBy == FILTER_CONTAINS ){
                    return( r1.firstName.indexOf( filterText ) 
                            >= 0 ||
                        r1.lastName.indexOf( filterText ) >= 0 );
                }
            }
            catch( Exception e ){
            }

            return false;
        }
    }

    // The options screen for choosing which sort and filter modes
    // to use, including the filter text

    class SortOptions extends Form {

        // Constructor

        SortOptions(){
            super( "Sort Options" );
            addCommand( okCommand );
            addCommand( cancelCommand );
            setCommandListener( EnumDemo.this );

            append( sortTypes );
            append( filterTypes );
            append( filterText );
        }

        // Return current sort type

        public int getSortType(){
            return sortTypes.getSelectedIndex();
        }

        // Return current filter type

        public int getFilterType(){
            return filterTypes.getSelectedIndex();
        }

        // Return current filter text

        public String getFilterText(){
            return filterText.getString();
        }

        // Labels and user interface components

        private String[] sortLabels = 
                         { "None", "First Last", "Last, First" };

        private String[] filterLabels = 
                           { "None", "Starts With", "Contains" };

        private ChoiceGroup sortTypes = new ChoiceGroup( 
                                                    "Sort Type:",
                                           ChoiceGroup.EXCLUSIVE,
                                              sortLabels, null );

        private ChoiceGroup filterTypes = new ChoiceGroup( 
                                                  "Filter Type:",
                                           ChoiceGroup.EXCLUSIVE,
                                            filterLabels, null );

        private TextField filterText = new TextField( 
                                                  "Filter Text:",
                                                   null, 20, 0 );
    }
}
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

- NOTE

Sun respects your online time and privacy. The Java Developer 
Connection mailing lists are used for internal Sun 
Microsystems(tm) purposes only. You have received this email 
because you elected to subscribe. To unsubscribe, go to the 
Subscriptions page (http://developer.java.sun.com/subscription/), 
uncheck the appropriate checkbox, and click the Update button.

As of May  22, 2001, Sun Microsystems updated its Privacy Policy 
(http://sun.com/privacy) to give you a better understanding of 
Sun's Privacy Policy and Practice. If you have any questions, 
contact privacy@sun.com.

- SUBSCRIBE

To subscribe to a JDC newsletter mailing list, go to the 
Subscriptions page (http://developer.java.sun.com/subscription/), 
choose the newsletters you want to subscribe to, and click Update.


- FEEDBACK
Comments? Send your feedback on the J2ME Tech Tips to:

jdc-webmaster@sun.com


- ARCHIVES
You'll find the J2ME Tech Tips archives at:

http://java.sun.com/jdc/J2METechTips/index.html

- COPYRIGHT
Copyright 2001 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright. For more information, 
see:

http://java.sun.com/jdc/copyright.html


- LINKS TO NON-SUN SITES
The J2ME Tech Tips may provide, or third parties may provide, 
links to other Internet sites or resources. Because Sun has no 
control over such sites and resources, You acknowledge and 
agree that Sun is not responsible for the availability of such 
external sites or resources, and does not endorse and is not 
responsible or liable for any Content, advertising, products, 
or other materials on or available from such sites or resources. 
Sun will not be responsible or liable, directly or indirectly, 
for any damage or loss caused or alleged to be caused by or in 
connection with use of or reliance on any such Content, goods or 
services available on or through any such site or resource.

J2ME Tech Tips 
June 22, 2001

Sun, Sun Microsystems, Java, Java Developer Connection, and 
J2ME and are trademarks or registered trademarks of 
Sun Microsystems, Inc. in the United States and other countries.




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -