日期:2014-05-20  浏览次数:20910 次

J2ME实现的通讯录程式

???? 本程序主要利用在J2ME MIDP的RMS功能,演示了 如何通过 RecordStore 对象进行存储数据 ,并实现一个通讯录程序,程序的主要代码如下:

AddressBook.java 的源代码如下:

java 代码
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.rms.*;
    
    
    public class AddressBook extends MIDlet implements CommandListener
    {
    	private Display display=null;
    	Command search=null;
    	Command quit=null;
    	Command delete=null;
    	Command addnow=null;	
    	Command edit=null;
    	Command mainmenu=null;
    	
    	List menu=null;
    	Form ui_form=null;
    	StringItem si=null;
    	TextField name=null;
    	TextField phone=null;
    	TextField nameForEdit=null;
    	
    	RecordStore recordStore=null;//声明记录储存
    	
    	public AddressBook()
    	{
    		display=Display.getDisplay(this);  //定义Display变量
    		quit=new Command("关闭",Command.SCREEN,3);//定义关闭按钮
    		search=new Command("查找",Command.SCREEN,2);//定义查询按钮
    		delete=new Command("删除",Command.SCREEN,2);//定义删除按钮
    		addnow=new Command("增加",Command.SCREEN,2);//定义增加按钮
    		edit=new Command("修改",Command.SCREEN,2);//定义修改按钮
    		mainmenu=new Command("主菜单",Command.SCREEN,2);//定义主菜单按钮
    	        //记录储存初始化
    		try 
    		{
    			recordStore=RecordStore.openRecordStore("address",true);
    		}
    		catch(RecordStoreException rse)
    		{
    			rse.printStackTrace();
    		}
    	}
    	public void startApp()
    	{
    		menu=new List("电话薄...",List.IMPLICIT);
    		menu.append("1.查询",null);
    		menu.append("2.增加",null);
    		menu.append("3.删除",null);
    		menu.append("4.修改",null);
    		menu.append("5.关闭",null);
    		menu.setCommandListener(this);
    		display.setCurrent(menu);
    		}
    	void searchScreen()  //查询用户界面
    	{
    		ui_form=new Form("电话薄查询");
    		 name=new TextField("查询姓名","",50,0);
    		ui_form.append(name);
    		
    		ui_form.addCommand(search);
    		ui_form.addCommand(quit);
    		
    		ui_form.setCommandListener(this);
    		
    		display.setCurrent(ui_form);
    	}
        void addScreen()
        {
        	ui_form=new Form("添加");
    		
        	name=new TextField("姓名","",50,0);
    		ui_form.append(name);
    		
    		phone=new TextField("电话号码","",50,0);
    		ui_form.append(phone);
    		
    		ui_form.addCommand(addnow);
    		ui_form.addCommand(quit);
    		
    		ui_form.setCommandListener(this);
    		
    		display.setCurrent(ui_form);
        }
        void editScreen()
        {
        	ui_form=new Form("修改");
    		
        	nameForEdit=new TextField("需要修改的姓名","",50,0);
    		ui_form.append(nameForEdit);
    		
    		name=new TextField("姓名","",50,0);
    		ui_form.append(name);
    		
    		phone=new TextField("电话号码","",50,0);
    		ui_form.append(phone);
    				
    		ui_form.addCommand(edit);
    		ui_form.addCommand(quit);
    		
    		ui_form.setCommandListener(this);
    		
    		display.setCurrent(ui_form);
        }
        void deleteScreen()
        {
        	ui_form=new Form("删除");
    		
        			
    		name=new TextField("姓名","",50,0);
    		ui_form.append(name);
    		
    						
    		ui_form.addCommand(delete);
    		ui_form.addCommand(quit);
    		
    		ui_form.setCommandListener(this);
    		
    		display.setCurrent(ui_form);
        }
    public void pauseApp()
    {
    	menu=null;
    }
    public void destroyApp(boolean unconditional)
    {
    	menu=null;
    	notifyDestroyed();
    }
    public void commandAction(Command c,Displayable d)
    {
    	if(c==quit)
    	{
    		try
    		{
    			close();
    		}
    	
    		catch(RecordStoreException rse)
    		{
    			rse.printStackTrace();
    		}
    		destroyApp(true);
    		}
    
    else if(c==search)
    {
    	String temp_search=name.getString();
    	address_search(temp_search);
    	
    }
    else if(c==edit)
    {
    	String temp_nameForEdit=nameForEdit.getString();
    	String temp_name=name.getString();
    	String temp_phone=phone.getString();
    	address_edit(temp_nameForEdit,temp_name,temp_phone);
    	}
    else if(c==mainmenu)
    {
    	startApp();
    }
    else if (c==delete)
    {
    	String temp_delete=name.getString();
    	address_del(temp_delete);
    }
    else if(c==addnow)
    {
    	String temp_name=name.getString();
    	String temp_phone=phone.getString();
    	address_add(temp_name,temp_phone);
    }
    else   //其他事件,根据list选项调用个界面
    {
    	List down=(List)display.getCurrent();
    	switch(down.getSelectedIndex())
    	{
    	case 0: searchScreen();break;
    	case 1: addScreen();break;
    	case 2: deleteScreen();break;
    	case 3: editScreen();break;
    	case 4: destroyApp(true);break;
    	}
    }
    }
    void address_search(String address)
    {
    	String temp=" ";
    	String phone_number;
    	String person_name;
    	String check_name="";
    	int size=address.length();
    	try
    	{
    		RecordEnumeration re=
    			recordStore.enumerateRecords(null,null,false); //enumeration通过项目集允许对例程进行重复调用。
    	    ui_form=new Form("查询结果");
    		while(re.hasNextElement())
    		{
    			String name1=new String(re.nextRecord());
    			try
    			{
    				person_name=name1.substring(0,name1.indexOf("?"));
    			}
    			catch(Exception ef)
    			{
    				person_name="请检查姓名是否正确";
    				ef.printStackTrace();
    			}
    			if(person_name.length()>=size)
    			{
    				check_name=person_name.substring(0,size);
    				
    			}
    			if(check_name.equals(address))
    			{
    				try{
    					phone_number=name1.substring(name1.indexOf("?")+1);
    					}
    				catch(Exception e)
    				{
    					phone_number="";
    				}
    		temp=temp+"\n姓名.."+person_name+"\n电话.."+phone_number;
    		}
    		}
    	//}
    
    if(temp.equals(" "))
    {
    	temp="没有找到......";
    }
    ui_form.append(temp);
    
    ui_form.addCommand(quit);
    ui_form.addCommand(mainmenu);
    ui_form.setCommandListener(this);
    display.setCurrent(ui_form);
    //}
    	}
    catch(RecordStoreNotOpenException rsnoe)
    {
    	rsnoe.printStackTrace();
    }
    catch(InvalidRecordIDException irid)
    {
    	irid.printStackTrace();
    	}
    catch(RecordStoreException rse)
    {
    	rse.printStackTrace();
    }
    } 
    void address_del(String address)
    {
    	String temp=" ";
    	String phone_number;
    	String person_name;
    	int id;
    	int del_id=0;
    	try
    	{   RecordEnumeration re=
    		recordStore.enumerateRecords(null,null,false);
    	    ui_form=new Form("删除结果");
    	while(re.hasNextElement())
    	{
    		id=re.nextRecordId();
    		String name1=new String(recordStore.getRecord(id));
    		try
    		{
    			person_name=name1.substring(0,name1.indexOf("?")); //搜索截取特定的字符串
    		}
    		catch(Exception ef)
    		{
    			person_name="请检查姓名是否正确";
    			ef.printStackTrace();
    		}
    		if(person_name.equals(address))
    		{
    			del_id=id;
    		}
    	}
    	if(del_id!=0)
    	{
    		recordStore.deleteRecord(del_id);
    		temp="成功地删除一条记录";
    		}
    	else
    	{
    		temp="所指定的记录不在电话薄内";
    	}
    }
    	catch(Exception e)
    	{
    	}
    	ui_form.append(temp);
    	
    	ui_form.addCommand(quit);
    	ui_form.addCommand(mainmenu);
    	
    	ui_form.setCommandListener(this);
    	
    	display.setCurrent(ui_form);
    	}
     void address_add(String address,String phone)
     {
     	String data=address+"?"+phone;
     	System.out.println(data);
     	try
    	{
     		byte[] b=data.getBytes();
     		recordStore.addRecord(b,0,b.length);
     		ui_form=new Form("添加成功");
     		ui_form.append("成功地添加一条记录");
     		
     		ui_form.addCommand(quit);
     		ui_form.addCommand(mainmenu);
     		
     		ui_form.setCommandListener(this);
     		
     		display.setCurrent(ui_form);
    	}
     	catch(RecordStoreException rse)
    	{
     		rse.printStackTrace();
    	}
     }
     	void address_edit(String addressForEdit,String address,String phone)
     	{
     		int id=1;
     		int edit_id=0;
     		String person_name;
     		String temp="error";
     		try
    		{
    		RecordEnumeration re=
    			recordStore.enumerateRecords(null,null,false);
    		    ui_form=new Form("修改");
    		while(re.hasNextElement())
    		{
    			id=re.nextRecordId();
    			String name1=new String(recordStore.getRecord(id));
    			try
    			{
    				person_name=name1.substring(0,name1.indexOf("?"));
    			}
    			catch(Exception ef)
    			{
    				person_name="请检查姓名是否正确";
    			}
    			if(person_name.equals(addressForEdit))
    			{
    				edit_id=id;
    			}
    		}
    		if(edit_id!=0)
    		{
    			String data=address+"?"+phone;
    			byte[] b_data=data.getBytes();
    			recordStore.setRecord(edit_id,b_data,0,b_data.length);
    			temp="成功地修改一条记录";
    			}
    		else
    		{
    			temp="要修改的记录不在电话薄内";
    		}
     	    }
    		catch(Exception e)
    		{
    		e.printStackTrace();
    		}
    		ui_form.append(temp);
     		
     		ui_form.addCommand(quit);
     		ui_form.addCommand(mainmenu);
     		
     		ui_form.setCommandListener(this);
     		display.setCurrent(ui_form);
     	}
        //关闭记录储存
        public void close()throws RecordStoreNotOpenException,RecordStoreException
       {
    	recordStore.closeRecordStore();
        }
     }
    
    ?