J2ME通讯录代码

TR>

花了两天时间搞定了通讯录,基本功能能实现

现在拿出来给大家一起交流,希望大家能找出BUG

截图如下

               

                     


              



                   

 

以下是代码:请保留作者版权,谢谢

 

 

 

 

 
//=====================MessageMIDlet主类=============================== 
package messagemidlet; 
//主类 
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
public class MessageMIDlet extends MIDlet  { 
  public static MessageMIDlet messagemidlet; 
  private MenuMIDLet menumidlet = new MenuMIDLet(); 
  /** Constructor */ 
  public MessageMIDlet() { 
    messagemidlet = this; 
  } 
  /** Main method */ 
  public void startApp() { 
    Display.getDisplay(this).setCurrent(menumidlet); 
  } 
  /** Handle pausing the MIDlet */ 
  public void pauseApp() { 
  } 
  /** Handle destroying the MIDlet */ 
  public void destroyApp(boolean unconditional) { 
  } 
  /** Quit the MIDlet */ 
  public static void quitApp() { 
    messagemidlet.destroyApp(true); 
    messagemidlet.notifyDestroyed(); 
    messagemidlet = null; 
  } 
} 
//=========================end================================ 

//=========================SetGet操作用到的属性================================ 
package messagemidlet; 
///JAVA为什么不像c#一样有GET SET 来设置属性,所以麻烦点,我还是喜欢C# 
public class SetGet { 

  public static int Id = 0; 
  public static int DelId = 0; 
  public static int ModId = 0; 
  public static String Str = null; 
  public static boolean IsOK =true ; 
  /** Constructor */ 
  public SetGet() { 
  } 
  public static void SetId(int id){ 
    Id = id; 
  } 
  public static int GetId(){ 
    return Id; 
  } 
  public static void SetDelId(int id) { 
    DelId = id; 
  } 
  public static int GetDelId() { 
    return DelId; 
  } 
  public static void SetModId(int id) { 
    ModId = id; 
  } 
  public static int GetModId() { 
    return ModId; 
  } 
  public static void SetStr(String str){ 
    Str = str; 
  } 
  public static String GetStr(){ 
    return Str; 
  } 
  public static void SetIsTrue(boolean istrue){ 
    IsOK = istrue; 
  } 
  public static boolean GetIsTrue(){ 
    return IsOK; 
  } 
  } 
//=========================end================================ 
//=========================PubClass公共类(集成一些常用属性和方法,方便调用)================================ 
package messagemidlet; 
import javax.microedition.lcdui.*; 
import javax.microedition.rms.*; 
import java.io.*; 
public class PubClass  { 
  public static PubClass pubclass; 
  public Display display; //舞台对象 
  public Command exitCommand; //离开按钮对象 
  public Command selectCommand; //选择按钮对象 
  public Command menuCommand;  //主目录 
  public Command okCommand ; 
  public Command operCommand; 
  public String[] menulist = {'通讯录','关于作者'}; //主菜单选项 
  public String[] mainlist ={'浏览联系人','查找联系人','添加联系人','其他'}; 
  public String[] operatelist={'编辑联系人','删除联系人','全部删除'};  //操作 
  //只需修改personpro属性,其他自动生成,构造 
  public String[] personpro ={'姓名','手机','Email','地址','其他'}; //用户属性 
  public int Length = personpro.length; //用户属性个数 
  public String dataname = 'messages'; //数据库名 
  public boolean existingOrNot = existing(dataname); 
  RecordStore rs = null; 
  public TextField[] person  = new TextField[Length]; 
  public StringItem[] persondetail = new StringItem[Length]; 
  /** 构造函数 */ 
  public PubClass() { 
    for(int i=0;i
  
   new  TextField(personpro[i], 
   '', 256, TextField.ANY); 
      persondetail[i] = 
   new  StringItem(personpro[i],
   ''); 
    } 
  } 

  
   //取得主目录列表 
  
   public String Get_MenuList(
   int i){ 
    
   if(
   this.mainlist.length<0){ 
      
   return 
   ''; 
    } 
    
   return 
   this.menulist[i]; 
  } 
 
   //取得通讯录列表 
  
   public String Get_MainList(
   int j){ 
    
   if(
   this.mainlist.length<0){ 
      
   return 
   ''; 
    } 
    
   return 
   this.mainlist[j]; 
  } 
  
   //取得联系人操作列表 
  
   public String Get_OperateList(
   int j){ 
  
   if(
   this.operatelist.length<0){ 
    
   return 
   ''; 
  } 
  
   return 
   this.operatelist[j]; 
} 

   //返回关于 

   public 
   void Return_AboutMIDlet() { 
  AboutMIDlet aboutmidlet = 
   new AboutMIDlet(); 
  display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(aboutmidlet); 
} 
 
   //返回主目录 
 
   public 
   void Return_MainMIDLet() { 
   MainMIDLet mainmidlet = 
   new MainMIDLet(); 
   display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(mainmidlet); 
 } 
 
   //返回通讯录列表 
  
   public 
   void Return_MenuMIDLet(){ 
    MenuMIDLet menumidlet = 
   new MenuMIDLet(); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(menumidlet); 
  } 
  
   //返回联系人列表 
  
   public 
   void Return_PersonListMIDLet(){ 
    PersonListMIDlet personlistmidlet = 
   new PersonListMIDlet(); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(personlistmidlet); 
  } 
  
   //返回查找联系人 
  
   public 
   void Return_SearchPersonMIDlet(){ 
  } 
  
   //返回添加联系人 
  
   public 
   void Return_AddPersonMIDLet(){ 
    AddPersonMIDLet addpersonmidlet = 
   new AddPersonMIDLet(); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(addpersonmidlet); 
  } 
  
   //返回操作 
  
   public 
   void Return_OperateMIDLet(){ 
    OperateMIDlet operatemidlet = 
   new OperateMIDlet(); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(operatemidlet); 
  } 
  
   //返回查找 
  
   public 
   void Return_SearchMIDlet() { 
    SearchMIDlet searrchmidlet = 
   new SearchMIDlet(); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(searrchmidlet); 
  } 
  
   //返回查找结果 
  
   public 
   void Return_SearchOutMIDlet() { 
    SearchOutMIDlet searrchoutmidlet = 
   new SearchOutMIDlet(); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(searrchoutmidlet); 
  } 
 
   //返回编辑 
  
   public 
   void Return_ModifyPersonMIDlet() { 
    ModifyPersonMIDlet modiftpersonmidlet = 
   new ModifyPersonMIDlet(); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(modiftpersonmidlet); 
  } 

  
   //返回联系人详细列表,并传入对应ID值 ModifyPersonMIDlet 
  
   public 
   void Return_PersonDetailMIDlet(){ 
    PersonDetailMIDlet persondetailmidlet = 
   new PersonDetailMIDlet(); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(persondetailmidlet); 
  } 
  
   //警告窗口 
  
   public 
   void Alert(String alertstring){ 
    Alert alert = 
   new Alert(
   '提示',alertstring,createImage(
   '/images/1.png'),AlertType.ERROR); 
    alert.setTimeout(2000); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(alert); 
  } 
  
   //警告窗口,提示后返回MainMIDLet 
  
   public 
   void Alert(String alertstring,MainMIDLet mainmidlet){ 
    Alert alert = 
   new Alert(
   '提示',alertstring,createImage(
   '/images/1.png'),AlertType.ERROR); 
    alert.setTimeout(2000); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(alert,mainmidlet); 
  } 
  
   //警告窗口,提示后返回PersonListMIDlet 
  
   public 
   void Alert(String alertstring, PersonListMIDlet Personlistmidlet) { 
    Alert alert = 
   new Alert(
   '提示', alertstring, createImage(
   '/images/1.png'),AlertType.ERROR); 
    alert.setTimeout(2000); 
    display.getDisplay(MessageMIDlet.messagemidlet).setCurrent(alert, Personlistmidlet); 
  } 

  
   //创建图像 
  
   public 
   static Image createImage(String name){ 
      Image aImage = 
   null; 
      
   try{ 
         aImage = Image.createImage(name); 
      } 
      
   catch(IOException e){ 
      } 
      
   return aImage; 
 } 

   //判断数据库是否存在,打开,然会RecordStore对象 
 
   public RecordStore Open(){ 
   boolean existingOrNot = 
   false; 
   boolean OK = 
   true; 
   existingOrNot = 
   this.existing(dataname); 
   
   if (existingOrNot) { 
     
   try { 
       rs = RecordStore.openRecordStore(dataname, 
   false); 
     } 
     
   catch (Exception e) { 
       OK = 
   false; 
     } 
   } 
     
   else { 
       
   try { 
         rs = RecordStore.openRecordStore(dataname, 
   true); 
         OK = 
   true; 
       } 
       
   catch (Exception e) { 
         OK = 
   false; 
       } 
     } 
       
   return rs; 
   } 
 
   //判断字符大小 
 
   public boolean existing(String recordStoreString) { 
   boolean existingOrNot = 
   false; 
   RecordStore rs = 
   null; 
   
   if (recordStoreString.length() > 32) 
     
   return 
   false; 
   
   try { 
     rs = RecordStore.openRecordStore(recordStoreString, 
   false); 
   } 
   
   catch (RecordStoreNotFoundException e) { 
     existingOrNot = 
   false; 
   } 
   
   catch (Exception e) { 
   } 
   
   finally { 
     
   try { 
       rs.closeRecordStore(); 
     } 
     
   catch (Exception e) { 
     } 
   } 
   
   return existingOrNot; 
 } 
} 


   //=========================end================================ 


   //=========================PersonClass用户属性类================================ 
package messagemidlet; 

   //联系人属性类 
import java.io.*; 
import javax.microedition.rms.*; 

   public 
   class PersonClass  { 
  
   private 
   static PersonClass personclass; 
  
   //构造公共类实例 
  
   private PubClass pubclass = 
   new PubClass(); 
  
   public 
   static 
   int Id = -1; 
   //设置静态ID,赋值后不需要重新构造而不能调用 
  
   //联系人属性,数组 
  
   public String[] PersonPro = 
   new String[pubclass.Length] ; 
  
   public 
   void Write(String[] PersonPro){ 
    
   for(
   int i = 0;i
   
    this.PersonPro[i] = PersonPro[i]; 
    } 
  } 
  
    //设置ID,起修改,删除,显示作用 
  
    public 
    void SetRecordID(
    int Id){ 
    
    this.Id = Id; 
  } 
  
    //取得ID 
  
    public 
    static 
    int GetRecordID(){ 
    
    return Id; 
  } 
  
    //转换为字节 
  
    public 
    byte[] changeToByteArray(){ 
  
    byte[] data = 
    null; 
  
    try{ 
    ByteArrayOutputStream baos = 
    new ByteArrayOutputStream(); 
    DataOutputStream dos = 
    new DataOutputStream(baos); 
    
    for(
    int j=0;j<
    this.PersonPro.length;j++){ 
      dos.writeUTF(
    this.PersonPro[j]); 
    } 
    data = baos.toByteArray(); 
    baos.close(); 
    dos.close(); 
  } 
  
    catch(Exception e){ 
    
    return 
    null; 
  } 
  
    return data; 
  } 
  
    public 
    void changeFromByteArray(
    byte[] data) { 
    
    try { 
      ByteArrayInputStream bais = 
    new ByteArrayInputStream(data); 
      DataInputStream dis = 
    new DataInputStream(bais); 
      
    for(
    int k=0;k
    
     this.PersonPro[k] = dis.readUTF(); 
      } 
      bais.close(); 
      dis.close(); 
    } 
    
     catch (Exception e) { 
    } 
  } 
  
     public String getPerson(
     int m) { 
    
     return 
     this.PersonPro[m]; 
  } 
  
     public String GetListName(
     int RecordId){ 
    
     return 
     this.PersonPro[0]; 
  } 

} 


     //=========================end================================ 



     //=========================MenuMIDLet主目录里表类================================ 
package messagemidlet; 

import javax.microedition.lcdui.*; 
import java.io.*; 

     public 
     class MenuMIDLet extends List implements CommandListener { 
  
     //构造公共类实例 
  
     private PubClass pubclass = 
     new PubClass(); 
  
     /** Constructor */ 
  
     public MenuMIDLet() { 
    super(
     '通讯录',List.IMPLICIT); 
    
     for(
     int i=0;i
     
      this.append(pubclass.Get_MenuList(i),pubclass.createImage(
      '/images/tel.png')); 
    } 
    
      //构造按钮 
    pubclass.selectCommand = 
      new Command(
      '选择', Command.SCREEN, 2); 
    pubclass.exitCommand = 
      new Command(
      '离开', Command.EXIT, 1); 
    
      //添加监听 
    
      this.addCommand(pubclass.exitCommand); 
    
      this.addCommand(pubclass.selectCommand); 
    
      this.setCommandListener(
      this); 
  } 
  
      public 
      void commandAction(Command c, Displayable d) { 
    
      if(c == pubclass.selectCommand ){ 
      
      int selectitem = 
      this.getSelectedIndex(); 
     
      //尽量不使用Switch,用If提高速度,虽然麻烦点,经验所得 
     
      if (selectitem == 0) { 
       pubclass.Return_MainMIDLet(); 
     } 
     
      if (selectitem == 1) { 
       pubclass.Return_AboutMIDlet(); 
     } 
   } 
    
      //退出 
    
      if(c == pubclass.exitCommand){ 
      MessageMIDlet.quitApp(); 
    } 
  } 
} 

      //=========================end================================ 


      //=========================AboutMIDlet关于作者类(显示作者信息)================================ 
package messagemidlet; 
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import javax.microedition.lcdui.Command; 
import javax.microedition.lcdui.Displayable; 

      public 
      class AboutMIDlet  extends Form implements CommandListener { 
  
      //构造公共类实例 
  
      private PubClass pubclass = 
      new PubClass(); 
  
      /** Constructor */ 
  
      public AboutMIDlet() { 
    super(
      ''); 
    
      this.setTitle(
      '关于作者'); 
    StringItem stringitem = 
      new StringItem(
      '作者介绍',
      '01网通(2)02号. 姓名:Sukey. 版权所有:Sukey.'); 
    
      this.append(stringitem); 
    
      //构造按钮 
    pubclass.menuCommand = 
      new Command(
      '主目录', Command.SCREEN, 1); 
    pubclass.exitCommand = 
      new Command(
      '离开', Command.BACK, 1); 
    
      //添加监听 
    
      this.addCommand(pubclass.menuCommand); 
    
      this.addCommand(pubclass.exitCommand); 
    
      this.setCommandListener(
      this); 
  } 
  
      public 
      void commandAction(Command c, Displayable d) { 
    
      if(c ==pubclass.menuCommand  ){ 
      pubclass.Return_MenuMIDLet(); 
    } 
    
      if(c == pubclass.exitCommand){ 
       MessageMIDlet.quitApp(); 
    } 
  } 
} 

      //=========================end================================ 


      //=========================MainMIDLet通讯录里表类================================ 

package messagemidlet; 

      // 
import javax.microedition.lcdui.*; 

      public 
      class MainMIDLet extends List implements CommandListener { 
  
      //构造公共类实例 
  
      private PubClass pubclass  = 
      new PubClass(); 
  
      /** Constructor */ 
  
      public MainMIDLet() { 
    super(
      '通讯录',List.IMPLICIT); 
    
      //显示列表 
    
      for(
      int i=0;i
      
       this.append(pubclass.Get_MainList(i),pubclass.createImage(
       '/images/tel.png')); 
    } 
    
       //构造按钮 
    pubclass.selectCommand = 
       new Command(
       '选择', Command.OK, 1); 
    pubclass.menuCommand = 
       new Command(
       '主目录', Command.EXIT, 2); 
   
       //添加监听 
    
       this.addCommand(pubclass.selectCommand); 
    
       this.addCommand(pubclass.menuCommand); 
    
       this.setCommandListener(
       this); 
  } 
  
       public 
       void commandAction(Command c, Displayable d) { 
    
       if (c == pubclass.selectCommand) { 
      
       int selectitem = 
       this.getSelectedIndex(); 
      
       //尽量不使用Switch,用If提高速度,虽然麻烦点,经验所得 
      
       if (selectitem == 0) { 
        pubclass.Return_PersonListMIDLet(); 
      } 
      
       if (selectitem == 1) { 
        pubclass.Return_SearchMIDlet(); 
      } 
      
       if (selectitem == 2) { 
        pubclass.Return_AddPersonMIDLet(); 
      } 
      
       if (selectitem == 3) { 
      } 

    } 
    
       if(c == pubclass.menuCommand){ 
      pubclass.Return_MenuMIDLet(); 
    } 
  } 
} 

       //=========================end================================ 


       //=========================AddPersonMIDLet添加联系人类================================ 
package messagemidlet; 
import javax.microedition.lcdui.*; 
import javax.microedition.rms.*; 

       public 
       class AddPersonMIDLet extends Form implements CommandListener { 
  
       //构造公共类实例 
  
       private PubClass pubclass = 
       new PubClass(); 
  
       /** Constructor */ 
  
       public AddPersonMIDLet() { 
    super(
       ''); 
    
       this.setTitle(
       '添加联系人'); 
     
       for(
       int i=0;i
       
        this.append(pubclass.person[i]); } 
        //构造按钮 pubclass.exitCommand = 
        new Command(
        '返回', Command.OK, 1); pubclass.okCommand = 
        new Command(
        '保存', Command.BACK, 2); 
        //添加监听 
        this.addCommand(pubclass.exitCommand); 
        this.addCommand(pubclass.okCommand); 
        this.setCommandListener(
        this); } 
        //添加记录 
        private 
        void AddRecord(){ RecordStore rs =
        null; rs = pubclass.Open(); 
        try { 
        //取得textfield每个直,以数组形式保存 String[] getstring = 
        new String[pubclass.Length]; 
        for(
        int i=0;i
        
         //构造personclass实例 PersonClass personclass = 
         new PersonClass(); personclass.Write(getstring); 
         byte[] data = personclass.changeToByteArray(); 
         int recordId = personclass.GetRecordID(); 
         if (recordId != -1) { rs.setRecord(recordId, data, 0, data.length); } 
         else { 
         //添加记录 recordId = rs.addRecord(data, 0, data.length); } SetGet.SetDelId(0); MainMIDLet mainmidlet = 
         new MainMIDLet(); pubclass.Alert(
         '添加成功!返回中.......', mainmidlet); } 
         catch (Exception e) { pubclass.Alert(
         '添加联系人失败'); } } 
         public 
         void commandAction(Command c, Displayable d) { 
         if(c == pubclass.okCommand){ 
         //判断至少要填写姓名 
         if(pubclass.person[0].getString() == 
         null || pubclass.person[0].getString() == 
         '' ){ pubclass.Alert(
         '至少要填写姓名'); }
         else{ 
         //添加记录 AddRecord(); } } 
         if(c == pubclass.exitCommand){ pubclass.Return_MainMIDLet(); } } } 
         //=========================end================================ 
         //=========================PersonListMIDlet联系人列表类(显示联系人列表)================================ package messagemidlet; import javax.microedition.lcdui.*; import javax.microedition.rms.*; 
         public 
         class PersonListMIDlet extends List implements CommandListener { 
         //构造公共类实例 
         private PubClass pubclass = 
         new PubClass(); 
         private PersonClass personclass = 
         new PersonClass(); 
         public 
         int[] recordIDs; 
         /** Constructor */ 
         public PersonListMIDlet() { super(
         '联系人列表',List.IMPLICIT); PersonList(); 
         //构造按钮 pubclass.selectCommand = 
         new Command(
         '查看', Command.SCREEN, 2); pubclass.exitCommand = 
         new Command(
         '编辑', Command.EXIT, 1); 
         //添加监听 
         this.addCommand(pubclass.exitCommand); 
         this.addCommand(pubclass.selectCommand); 
         this.setCommandListener(
         this); } 
         //用户列表 
         public 
         void PersonList() { RecordStore rs = 
         null; rs = pubclass.Open(); 
         try { 
         byte[] data; 
         //建立枚举,取出记录 RecordEnumeration re = rs.enumerateRecords(
         null, 
         null, 
         false); 
         //判断是否又记录 
         if (re.hasNextElement()) { 
         int records=re.numRecords();
         //取得记录数 recordIDs=
         new 
         int[records];
         //recordID记录数组 
         for(
         int i=0;i
         
          //保存id data = rs.getRecord(recordIDs[i]); personclass.changeFromByteArray(data); 
          this.append(
          ''+personclass.getPerson(0)+
          '',pubclass.createImage(
          '/images/per.png'));
          //插入此record名称 } SetGet.SetIsTrue(
          true); } 
          else { 
          this.append(
          '还未添加联系人?', 
          null); SetGet.SetIsTrue(
          false); } } 
          catch (Exception e) { } } 
          //全局变量ID 
          private 
          void SetGetId() { 
          int itemid = 
          this.getSelectedIndex(); SetGet.SetId(recordIDs[itemid]); } 
          //全局变量DelID 
          private 
          void SetGetDelId() { 
          int itemid = 
          this.getSelectedIndex(); SetGet.SetDelId(recordIDs[itemid]); } 
          private 
          void SetGetModId(){ 
          int itemid = 
          this.getSelectedIndex(); SetGet.SetModId(recordIDs[itemid]); } 
          //提示信息并返回 
          public 
          void AlerNone(){ MainMIDLet mainmidlet = 
          new MainMIDLet(); pubclass.Alert(
          '无法操作',mainmidlet); } 
          public 
          void commandAction(Command c, Displayable d) { 
          if(c ==pubclass.exitCommand ){ 
          if (SetGet.GetIsTrue()){ 
          this.SetGetDelId(); 
          this.SetGetModId(); pubclass.Return_OperateMIDLet(); }
          else{ AlerNone(); } } 
          if(c ==pubclass.selectCommand ){ 
          if (SetGet.GetIsTrue()){ 
          this.SetGetId(); pubclass.Return_PersonDetailMIDlet(); }
          else{ AlerNone(); } } } } 
          //=========================end================================ 
          //=========================SearchMIDlet查找类================================ package messagemidlet; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Displayable; 
          public 
          class SearchMIDlet extends TextBox implements CommandListener{ 
          //构造公共类实例 
          private PubClass pubclass = 
          new PubClass(); 
          /** Constructor */ 
          public SearchMIDlet() { super(
          '查找联系人',
          '',256,0); 
          //构造按钮 pubclass.menuCommand = 
          new Command(
          '查找', Command.SCREEN, 1); pubclass.okCommand = 
          new Command(
          '主目录', Command.BACK, 1); 
          //添加监听 
          this.addCommand(pubclass.menuCommand); 
          this.addCommand(pubclass.okCommand); 
          this.setCommandListener(
          this); } 
          public 
          void commandAction(Command c, Displayable d) { 
          if(c ==pubclass.okCommand ){ pubclass.Return_MenuMIDLet(); } 
          if(c ==pubclass.menuCommand){ 
          //设置全局变量 SetGet.SetStr(
          this.getString()); pubclass.Return_SearchOutMIDlet(); } } } 
          //=========================end================================ 
          //=========================SearchOutMIDlet查找结果类================================ package messagemidlet; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import java.io.*; 
          public 
          class SearchOutMIDlet extends List implements CommandListener { 
          //构造公共类实例 
          private PubClass pubclass = 
          new PubClass(); 
          private PersonClass personclass = 
          new PersonClass(); 
          public 
          int[] recordIDs; 
          /** Constructor */ 
          public SearchOutMIDlet() { super(
          '查找结果', List.IMPLICIT); PersonList(); 
          //构造按钮 pubclass.selectCommand = 
          new Command(
          '查看', Command.SCREEN, 2); pubclass.exitCommand = 
          new Command(
          '编辑', Command.EXIT, 1); 
          //添加监听 
          this.addCommand(pubclass.exitCommand); 
          this.addCommand(pubclass.selectCommand); 
          this.setCommandListener(
          this); } 
          //用户列表 
          public 
          void PersonList() { RecordStore rs = 
          null; rs = pubclass.Open(); 
          try { 
          byte[] data; 
          //匹配 RecordFilter rf = 
          new nameFilter(); 
          //建立枚举,取出记录 RecordEnumeration re = rs.enumerateRecords(rf, 
          null, 
          false); 
          //判断是否又记录 
          if (re.hasNextElement()) { 
          int records=re.numRecords();
          //取得记录数 recordIDs=
          new 
          int[records];
          //recordID记录数组 
          for(
          int i=0;i
          
           //保存id data = rs.getRecord(recordIDs[i]); personclass.changeFromByteArray(data); 
           this.append(
           ''+personclass.getPerson(0)+
           '',pubclass.createImage(
           '/images/per.png'));
           //插入此record名称 } SetGet.SetIsTrue(
           true); } 
           else { 
           this.append(
           '对不起,没有查找到', 
           null); SetGet.SetIsTrue(
           false); } } 
           catch (Exception e) { } } 
           //全局变量ID 
           private 
           void SetGetId() { 
           int itemid = 
           this.getSelectedIndex(); SetGet.SetId(recordIDs[itemid]); } 
           //全局变量ModID 
           private 
           void SetGetModId() { 
           int itemid = 
           this.getSelectedIndex(); SetGet.SetModId(recordIDs[itemid]); } 
           //全局变量DelID 
           private 
           void SetGetDelId() { 
           int itemid = 
           this.getSelectedIndex(); SetGet.SetDelId(recordIDs[itemid]); } 
           //提示信息并返回 
           public 
           void AlerNone(){ MainMIDLet mainmidlet = 
           new MainMIDLet(); pubclass.Alert(
           '无法操作',mainmidlet); } 
           public 
           void commandAction(Command c, Displayable d) { 
           if(c ==pubclass.exitCommand ){ 
           if (SetGet.GetIsTrue()){ 
           this.SetGetModId(); 
           this.SetGetDelId(); pubclass.Return_OperateMIDLet(); }
           else{ AlerNone(); } } 
           if(c ==pubclass.selectCommand ){ 
           if (SetGet.GetIsTrue()){ 
           this.SetGetId(); pubclass.Return_PersonDetailMIDlet(); }
           else{ AlerNone(); } } } } 
           class nameFilter implements RecordFilter{ 
           public boolean matches(
           byte[] candidate){ DataInputStream person = 
           new DataInputStream(
           new ByteArrayInputStream(candidate)); String personcname = 
           ''; 
           try{ personcname = person.readUTF(); } 
           catch(Exception e){ } PersonClass personclass = 
           new PersonClass(); 
           if(personcname.equals(SetGet.GetStr())) 
           return 
           true; 
           else 
           return 
           false; } } 
           //=========================end================================ 
           //=========================PersonDetailMIDlet指定联系人详细信息================================ package messagemidlet; import javax.microedition.lcdui.*; import javax.microedition.rms.*; 
           public 
           class PersonDetailMIDlet extends Form implements CommandListener { 
           //构造公共类实例 
           private PubClass pubclass = 
           new PubClass(); 
           /** Constructor */ 
           public PersonDetailMIDlet() { super(
           ''); 
           this.ConstructorList(); 
           //构造按钮 pubclass.selectCommand = 
           new Command(
           '返回', Command.SCREEN, 2); pubclass.exitCommand = 
           new Command(
           '主目录', Command.EXIT, 1); 
           //添加监听 
           this.addCommand(pubclass.exitCommand); 
           this.addCommand(pubclass.selectCommand); 
           this.setCommandListener(
           this); } 
           //构造LIST,显示详细信息 
           private 
           void ConstructorList() { 
           try { 
           this.GetPersonDetail(); } 
           catch (RecordStoreException ex) { } } 
           //绑定对应ID数据,并显示 
           private 
           void GetPersonDetail() throws RecordStoreException { RecordStore rs = 
           null; 
           //打开数据库 rs = pubclass.Open(); 
           //建立枚举,取得对应ID的详细信息 
           try { 
           byte[] data; RecordEnumeration re = rs.enumerateRecords(
           null, 
           null, 
           false); data = rs.getRecord(SetGet.GetId()); 
           //重新构造,并把byte转换为string类型 PersonClass personclass = 
           new PersonClass(); personclass.changeFromByteArray(data); 
           //重新设置用户ID 
           int RecordId = rs.getNextRecordID(); 
           //给对应的属性赋值 
           for (
           int i = 0; i < pubclass.persondetail.length; i++) { 
           this.append(pubclass.persondetail[i]); pubclass.persondetail[i].setText(personclass.getPerson(i)); } 
           //设置标题为用户名 
           this.setTitle(personclass.getPerson(0)); } 
           catch (RecordStoreNotOpenException ex) { } } 
           public 
           void commandAction(Command c, Displayable d) { 
           if(c == pubclass.selectCommand){ pubclass.Return_PersonListMIDLet(); } 
           if(c == pubclass.exitCommand){ pubclass.Return_MenuMIDLet(); } } } 
           //=========================end================================ 
           //=========================OperateMIDlet操作联系人类(这里只有删除功能)================================ package messagemidlet; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; 
           public 
           class OperateMIDlet extends List implements CommandListener { 
           //构造公共类实例 
           private PubClass pubclass = 
           new PubClass(); 
           /** Constructor */ 
           public OperateMIDlet() { super(
           '联系人', List.IMPLICIT); 
           for (
           int i = 0; i < pubclass.operatelist.length; i++) { 
           this.append(pubclass.Get_OperateList(i), pubclass.createImage(
           '/images/edit.png')); } 
           //构造按钮 pubclass.operCommand = 
           new Command(
           '选择', Command.SCREEN, 2); pubclass.menuCommand = 
           new Command(
           '主目录', Command.EXIT, 1); 
           //添加监听 
           this.addCommand(pubclass.menuCommand); 
           this.addCommand(pubclass.operCommand); 
           this.setCommandListener(
           this); } 
           //删除指定记录 
           public 
           void Del(
           int delid) { PersonClass personclass = 
           new PersonClass(); RecordStore rs = 
           null; rs = pubclass.Open(); 
           try { 
           //判断是否又记录 
           if(rs.getNumRecords()<1){ pubclass.Alert(
           '还未添加记录!返回中.......'); }
           else{ 
           //bug rs.deleteRecord(delid); AlertInfo(
           '删除成功!返回中.......'); } } 
           catch (Exception e) { AlertInfo(
           '删除失败!返回中.......'); } } 
           //全部删除记录 
           public 
           void Delall() { PersonClass personclass = 
           new PersonClass(); RecordStore rs = 
           null; rs = pubclass.Open(); 
           //判断是否又记录 
           try { 
           if (rs.getNumRecords() < 1) { pubclass.Alert(
           '还未添加记录!返回中.......'); } 
           else { 
           //构造RecordEnumeration实例,取出记录 RecordEnumeration re = rs.enumerateRecords(
           null, 
           null, 
           false); 
           //根据ID,循环删除 
           while (re.hasNextElement()) { 
           int recoidId = re.nextRecordId(); rs.deleteRecord(recoidId); } AlertInfo(
           '删除成功!返回中.......'); } } 
           catch (Exception e) { AlertInfo(
           '删除失败!返回中.......'); } } 
           public 
           void AlertInfo(String info){ PersonListMIDlet Personlistmidlet = 
           new PersonListMIDlet(); pubclass.Alert(in, fo,Personlistmidlet); } 
           public 
           void commandAction(Command c, Displayable d) { 
           if(c ==pubclass.menuCommand ){ pubclass.Return_MenuMIDLet(); } 
           if(c ==pubclass.operCommand ){ 
           int selectitem = 
           this.getSelectedIndex(); 
           //尽量不使用Switch,用If提高速度,虽然麻烦点,经验所得 
           if (selectitem == 0) { pubclass.Return_ModifyPersonMIDlet(); } 
           if (selectitem == 1) { Del(SetGet.GetDelId()); SetGet.SetDelId(0); } 
           if (selectitem == 2) { Delall(); SetGet.SetDelId(0); } } } } 
           //=========================end================================ 
           //=========================ModifyPersonMIDlet修改指定联系人详细信息================================ package messagemidlet; import javax.microedition.lcdui.*; import javax.microedition.rms.*; 
           public 
           class ModifyPersonMIDlet extends Form implements CommandListener { 
           //构造公共类实例 
           private PubClass pubclass = 
           new PubClass(); 
           /** Constructor */ 
           public ModifyPersonMIDlet() { super(
           ''); 
           try { 
           this.GetPersonDetail(); } 
           catch (RecordStoreException ex) { } 
           //构造按钮 pubclass.exitCommand = 
           new Command(
           '主目录', Command.OK, 1); pubclass.okCommand = 
           new Command(
           '修改', Command.BACK, 2); 
           //添加监听 
           this.addCommand(pubclass.exitCommand); 
           this.addCommand(pubclass.okCommand); 
           this.setCommandListener(
           this); } 
           //绑定对应ID数据,并显示 
           private 
           void GetPersonDetail() throws RecordStoreException { RecordStore rs = 
           null; 
           //打开数据库 rs = pubclass.Open(); 
           //建立枚举,取得对应ID的详细信息 
           try { 
           byte[] data; RecordEnumeration re = rs.enumerateRecords(
           null, 
           null, 
           false); data = rs.getRecord(SetGet.GetModId()); 
           //重新构造,并把byte转换为string类型 PersonClass personclass = 
           new PersonClass(); personclass.changeFromByteArray(data); 
           //重新设置用户ID 
           int RecordId = rs.getNextRecordID(); 
           //给对应的属性赋值 
           for (
           int i = 0; i < pubclass.person.length; i++) { 
           this.append(pubclass.person[i]); pubclass.person[i].setString(personclass.getPerson(i)); } 
           //设置标题为用户名 
           this.setTitle(
           '修改'+personclass.getPerson(0)); } 
           catch (RecordStoreNotOpenException ex) { } rs.closeRecordStore(); } 
           //修改联系人 
           public 
           void UpdataPerson() { RecordStore rs = 
           null; rs = pubclass.Open(); 
           try { 
           //取得textfield每个直,以数组形式保存 String[] getstring = 
           new String[pubclass.Length]; 
           for (
           int i = 0; i < pubclass.Length; i++) { getstring[i] = pubclass.person[i].getString(); } 
           //构造personclass实例 PersonClass personclass = 
           new PersonClass(); personclass.Write(getstring); 
           byte[] data = personclass.changeToByteArray(); 
           int recordId = personclass.GetRecordID(); 
           if (recordId != -1) { rs.setRecord(recordId, data, 0, data.length); } 
           else { 
           //修改记录 rs.setRecord(SetGet.GetModId(),data,0,data.length);
           //rs.setRecord(SetGet.GetModId(), data,0, data.length); personclass.SetRecordID(recordId); } SetGet.SetModId(0); PersonListMIDlet Personlistmidlet = 
           new PersonListMIDlet(); pubclass.Alert(
           '修改成功!返回中.......',Personlistmidlet); } 
           catch (Exception e) { PersonListMIDlet Personlistmidlet = 
           new PersonListMIDlet(); pubclass.Alert(
           '修改失败',Personlistmidlet); } } 
           public 
           void commandAction(Command c, Displayable d) { 
           if(c == pubclass.exitCommand){ pubclass.Return_MenuMIDLet(); } 
           if(c == pubclass.okCommand){ 
           //判断至少要填写姓名 
           if (pubclass.person[0].getString() == 
           null || pubclass.person[0].getString() == 
           '') { pubclass.Alert(
           '至少要填写姓名'); } 
           else { 
           this.UpdataPerson(); } } } } 
           //=========================end================================ 
          
         
        
       
      
     
    
   
  

 

 

微信扫码订阅
UP更新不错过~
关注
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 5

打赏作者

sukey00

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值