当我尝试使用以下方式获取电话簿联系人时:
contactInfo.mobileNo = contactItem.getString(Contact.TEL,0);
我只在Phone number条目下获得数字,而不在Mobile number下显示条目。
如何获取手机号码下的电话簿条目?
您只获取一个元素,需要循环
contactItem.getString(Contact.TEL,0);
import java.util.Enumeration;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
String str = "";
try {
PIM pim = PIM.getInstance();
String[] names = pim.listPIMLists(PIM.CONTACT_LIST);
pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, names[0]);
ContactList addressbook = (ContactList) (pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY));
Contact contact = null;
String phoneNumber = null;
Enumeration items = null;
items = addressbook.items();
while (items.hasMoreElements()) {
contact = (Contact) items.nextElement();
int telCount = contact.countValues(Contact.TEL);
int nameCount = contact.countValues(Contact.FORMATTED_NAME);
if (telCount > 0 && nameCount > 0) {
String contactName = contact.getString(Contact.FORMATTED_NAME, 0);
System.out.println("\n\nName: "+contactName);
str += "\n\nName: "+contactName;
for (int i = 0; i < telCount; i++) {
phoneNumber = contact.getString(Contact.TEL, i);
System.out.println("\nNumber: "+phoneNumber);
str += "\nNumber: "+phoneNumber;
}
}
}
} catch (SecurityException e) {
} catch (Exception e) {
}