javaME 通讯录Contact
--------------------编程问答-------------------- 还是将源代码放上来吧public class ContactsManagerDemo extends MIDlet implements CommandListener {
PIM pim = PIM.getInstance();
Display display;
Command exitCommand,backCommand,saveCommand,deleteCommand;
List mainMenu,contactMenu;
Form contactInfoForm,newContactForm;
TextField givenNameField,familyNameField,telField,emailField,yearField,monthField,dayField;
Alert errorScreen,criticalErrorScreen;
Vector contactVec;//向量类
ContactList contactList;
Contact selectedContact;
public ContactsManagerDemo() {
exitCommand = new Command("退出",Command.EXIT,1);
backCommand = new Command("返回",Command.SCREEN,2);
saveCommand = new Command("保存",Command.SCREEN,2);
deleteCommand = new Command("删除",Command.SCREEN,2);
mainMenu = new List("通讯录管理器",Choice.IMPLICIT,new String[]{"全部联系人","新增联系人","退出"},null);
mainMenu.setCommandListener(this);
errorScreen = new Alert("","",null,AlertType.ERROR);
errorScreen.setTimeout(3000);
criticalErrorScreen = new Alert("","",null,AlertType.ERROR);
criticalErrorScreen.addCommand(exitCommand);
criticalErrorScreen.setCommandListener(this);
contactInfoForm = new Form("联系人信息");
contactInfoForm.addCommand(backCommand);
contactInfoForm.addCommand(deleteCommand);
contactInfoForm.setCommandListener(this);
newContactForm = new Form("新增联系人");
givenNameField = new TextField("名字","",16,TextField.ANY);
familyNameField = new TextField("姓氏","",16,TextField.ANY);
telField = new TextField("电话","",16,TextField.NUMERIC);
emailField = new TextField("邮件","",30,TextField.EMAILADDR);
yearField = new TextField("出生日期(年)","",4,TextField.NUMERIC);
monthField = new TextField("出生日期(月)","",4,TextField.NUMERIC);
dayField = new TextField("出生日期(日)","",4,TextField.NUMERIC);
newContactForm.append(givenNameField);
newContactForm.append(familyNameField);
newContactForm.append(telField);
newContactForm.append(yearField);
newContactForm.append(monthField);
newContactForm.append(dayField);
newContactForm.addCommand(saveCommand);
newContactForm.setCommandListener(this);
}
protected void destroyApp(boolean areg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(mainMenu);
}
public void commandAction(Command cmd, Displayable disp) {
try{
if(cmd == exitCommand){//退出
destroyApp(true);
notifyDestroyed();
}
else if(cmd == List.SELECT_COMMAND && disp == mainMenu){
switch(mainMenu.getSelectedIndex()){
case 0://显示全部联系人时
refresh();
break;
case 1://显示新增联系人时
givenNameField.setString("");
familyNameField.setString("");
telField.setString("");
emailField.setString("");
yearField.setString("");
monthField.setString("");
dayField.setString("");
display.setCurrent(newContactForm);
break;
case 2://显示退出时
destroyApp(true);
notifyDestroyed();
}
}
else if(cmd == List.SELECT_COMMAND && disp == contactMenu){
int selectedIndex = contactMenu.getSelectedIndex();
if(selectedIndex == contactVec.size()){
display.setCurrent(mainMenu);
}
else{
selectedContact = (Contact)contactVec.elementAt(selectedIndex);
contactInfoForm.deleteAll();
String[] names = selectedContact.getStringArray(Contact.NAME,PIMItem.ATTR_NONE);
String name = names[Contact.NAME_GIVEN] + "" + names[Contact.NAME_FAMILY];
String tel = selectedContact.getString(Contact.TEL,PIMItem.ATTR_NONE);
String email = selectedContact.getString(Contact.TEL, PIMItem.ATTR_NONE);
long dateOfBirth = selectedContact.getDate(Contact.BIRTHDAY, PIMItem.ATTR_NONE);
String birthday = date2String(dateOfBirth);
StringItem nameItem = new StringItem("姓名",name);
nameItem.setLayout(Item.LAYOUT_NEWLINE_AFTER);
StringItem telItem = new StringItem("电话",tel);
telItem.setLayout(Item.LAYOUT_NEWLINE_AFTER);
StringItem emailItem = new StringItem("电邮",email);
emailItem.setLayout(Item.LAYOUT_NEWLINE_AFTER);
StringItem birthdayItem = new StringItem("生日",birthday);
birthdayItem.setLayout(Item.LAYOUT_NEWLINE_AFTER);
contactInfoForm.append(nameItem);
contactInfoForm.append(telItem);
contactInfoForm.append(emailItem);
contactInfoForm.append(birthdayItem);
display.setCurrent(contactInfoForm);
}
}
else if(cmd == saveCommand){
String[] names = new String[((PIMList) contactList).stringArraySize(Contact.NAME)];
if(((PIMList) contactList).isSupportedArrayElement(Contact.NAME,Contact.NAME_FAMILY)){
names[Contact.NAME_FAMILY] = familyNameField.getString();
}
if(((PIMList) contactList).isSupportedArrayElement(Contact.NAME,Contact.NAME_GIVEN)){
names[Contact.NAME_GIVEN] = givenNameField.getString();
}
long dateOfBirth = date2long(yearField.getString(),monthField.getString(),dayField.getString());
Contact contact = ((Object) contactList).createContact();//创建一个联系人
contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, names);
if( ((PIMList) contactList).isSupportedField(Contact.TEL)){
contact.addString(Contact.TEL,PIMItem.ATTR_NONE,telField.getString());
}
if( ((PIMList) contactList).isSupportedField(Contact.EMAIL)){
contact.addString(Contact.EMAIL,PIMItem.ATTR_NONE,emailField.getString());
}
if( ((PIMList) contactList).isSupportedField(Contact.BIRTHDAY)){
contact.addDate(Contact.BIRTHDAY,PIMItem.ATTR_NONE,dateOfBirth);
}
contact.commit();
refresh();
}
else if(cmd == backCommand){
refresh();
}
else if(cmd == deleteCommand){
((Object) contactList).removeContact(selectedContact);
selectedContact = null;
refresh();
}
}catch(PIMException e){
criticalError("无法打开个人信息列表");
}catch(SecurityException e){
criticalError("当前访问不符合安全条件");
}
}
private void criticalError(String errorMessage){
criticalErrorScreen.setString(errorMessage);
display.setCurrent(criticalErrorScreen);
}
private void createContactMenu(ContactList contactList){
Enumeration contactEnu = null;
try{
contactEnu = ((PIMList) contactList).items();
}catch(PIMException e){
criticalError("无法打开个人信息列表");
}catch(SecurityException e){
criticalError("当前访问不符合安全条件");
}
contactVec = new Vector();
if(contactEnu != null){
while(contactEnu.hasMoreElements()){
contactVec.addElement(contactEnu.nextElement());
}
}
Contact[] contacts;
if(contactVec.size() > 0){
contacts = new Contact[contactVec.size()];
contactVec.copyInto(contacts);
}
else{
contacts = new Contact[0];
}
String[] contactMenuElements = new String[contacts.length + 1];
for(int i=0;i<contacts.length;i++){
Contact contact = contacts[i];
String[] names = contact.getStringArray(Contact.NAME,PIMItem.ATTR_NONE);
String tel = contact.getString(Contact.TEL,PIMItem.ATTR_NONE);
contactMenuElements[i] = names[Contact.NAME_GIVEN] + "" + names[Contact.NAME_FAMILY] + "" + " -- " + tel;
}
contactMenuElements[contacts.length] = "返回主菜单";
contactMenu = new List("",Choice.IMPLICIT,contactMenuElements,null);
contactMenu.setCommandListener(this);
}
private String date2String(long date){
Date d = new Date(date);
Calendar c = Calendar.getInstance();
c.setTime(d);
return c.get(Calendar.YEAR) + "/" + (c.get(Calendar.MONTH) + 1) + "/" + c.get(Calendar.DAY_OF_MONTH);
}
private long date2long(String year,String month,String day){
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,Integer.parseInt(year));
c.set(Calendar.MONTH,Integer.parseInt(month) - 1);
c.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
return c.getTime().getTime();
}
private void refresh() throws PIMException,SecurityException{
contactList = (ContactList)pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);//创建一个联系人列表
createContactMenu(contactList);
display.setCurrent(contactMenu);
}
}
--------------------编程问答-------------------- 求解啊啊
补充:Java , J2ME