Public class pimtest extends MIDlet implements commandlistener {
Private display;
Private form;
Private command exit;
Command OK;
Public pimtest (){
Display = display. getdisplay (this );
OK = new command ("read", command. OK, 1 );
Exit = new command ("exit", command. Exit, 1 );
Form = new form ("read Address Book ");
Form. addcommand (OK );
Form. addcommand (exit );
Form. setcommandlistener (this );
}
Protected void destroyapp (Boolean arg0 ){
}
Protected void pauseapp (){
}
Protected void Startapp () throws midletstatechangeexception {
Display. setcurrent (form );
}
/**
* Read the address book
*
*/
Private void readaddresslist (){
String [] names = Pim. getinstance (). listpimlists (PIM. contact_list); // get the contact list name
For (INT I = 0; I <names. length; I ++ ){
Try {
Form. append ("name =" + Names [I] + "/N ");
Contactlist list = (contactlist) Pim. getinstance (). openpimlist (
Pim. contact_list, Pim. read_only, Names [I]);
Contact contact;
Enumeration E = List. Items ();
Int n = 0; // number of records
While (E. hasmoreelements ()){
Contact = (contact) E. nextelement ();
String name = NULL;
Try {
// Obtain the name
String [] arraystr = contact. getstringarray (
Contact. Name, 0 );
If (arraystr! = NULL & arraystr. length! = 0 ){
Name = "";
// The first five subscripts are valid on n78 and correspond to: 0 surname, 1 Name, 2 intermediate name, 3 title, and 4 title.
For (Int J = 0; j <arraystr. length; j ++ ){
If (arraystr [J]! = NULL
& Arraystr [J]. Length ()! = 0 ){
Name + = J + "." + arraystr [J];
}
}
}
} Catch (exception exc ){
Form. append ("error:" + exc. tostring () + "/N ");
}
Try {
If (name = NULL) {// if the name is not obtained, change it to formatted_name.
If (list. issupportedfield (contact. formatted_name )){
Name = contact. getstring (
Contact. formatted_name, 0 );
}
}
} Catch (exception exc ){
Form. append ("error:" + exc. tostring () + "/N ");
}
Form. append ("name =" + name + "/N ");
If (list. issupportedfield (contact. Tel) {// obtain Tel
String Tel = contact. getstring (contact. Tel, 0 );
Form. append ("Tel =" + Tel + "/N ");
}
N ++; // The number of records increases
}
Form. append ("Total" + N + "records/N ");
List. Close ();
} Catch (pimexception e ){
Form. append ("pimexception:" + E. tostring ());
E. printstacktrace ();
} Catch (exception e ){
Form. append ("exception:" + E. tostring ());
E. printstacktrace ();
}
}
}
Public void commandaction (command C, displayable d ){
If (C = exit ){
Destroyapp (true );
Yydestroyed ();
} Else if (C = OK ){
Readaddresslist ();
}
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/kf156/archive/2009/09/14/4551131.aspx