我的J2ME编程联系(1)——List

类别:Java 点击:0 评论:0 推荐:

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

public class listtest extends MIDlet implements CommandListener{
 
 private List alist;
 private Command exitcommand;
        private Command okcommand;
 private Display adisplay;
        private Alert analert;
       
 
 public listtest(){
            analert=new Alert("Selected","You have Choose a Fruit",null,AlertType.INFO);
           

           alist=new List("fruit",Choice.MULTIPLE);
    exitcommand=new Command("Exit",Command.EXIT,1);
           okcommand=new Command("OK",Command.OK,1);
  
    alist.append("Apple",null);
    alist.append("Orange",null);
           alist.append("banana",null);
  
    alist.addCommand(exitcommand);
           alist.addCommand(okcommand);
  
    alist.setCommandListener(this);
 }
 
 public void startApp(){
  
  adisplay=Display.getDisplay(this);
  adisplay.setCurrent(alist);
  
 }
 
 public void pauseApp(){
  
 }
 
 public void destroyApp(boolean unconditional){
  
 }
 
 public void commandAction(Command c,Displayable d){
            // use the i get the Selected list element
            //int i =alist.getSelectedIndex();
           
            if(c==exitcommand){
                destroyApp(false);
  notifyDestroyed();
               
            }
  
                else {
                    //display different information when diffenent element
                    //have been selected
                    /*if(i==0){
                        analert.setString("You have Choosed Apple!");
                        adisplay.setCurrent(analert,alist);
                    }
                   
                    if(i==1){
                        analert.setString("You have Choosed Orange!");
                        adisplay.setCurrent(analert,alist);
                    }
                   
                    if(i==2){
                        analert.setString("You have Choosed Banana!");
                        adisplay.setCurrent(analert,alist);
                    }*/
                String s=" ";
                for(int i=0;i<alist.size();i++){
                   
                    boolean selected=alist.isSelected(i);
                    if(selected&&i==0){
                        s+="Apple ";
                    }
                   
                    if(selected&&i==1){
                        s+="Orange ";
                    }
                   
                    if(selected&&i==2){
                        s+="Banana ";
                    }
                }
               
                analert.setString("You have Selected"+s);
                analert.setTimeout(Alert.FOREVER);
                adisplay.setCurrent(analert,alist);
               
                       
                }
               
               
  
 }
   
 
 
}

本文地址:http://com.8s8s.com/it/it10234.htm