我正在尝试使用此代码来检索联系人号码和联系人姓名。我得到了数字,但名称列表返回null
ContentResolver cr = this.getContentResolver(); //Activity/Application android.content.Context
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst())
{
List<String> ContactNames = new ArrayList<String>();
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
while (pCur.moveToNext())
{
String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactNames = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
ContactNames.add(contactNames);
break;
}
pCur.close();
}
} while (cursor.moveToNext()) ;
}
我不知道我错过了什么。请帮忙
答案 0 :(得分:0)
阅读联系 电话列表:
import java.util.ArrayList;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
public class ContactsUtils {
public static ArrayList<ContactModel> getContactList(Context ctx) {
ArrayList<ContactModel> list = new ArrayList<ContactModel>();
String displayName = "", phoneNo = "", email = " ";
ContentResolver contentResolver = ctx.getContentResolver();
Cursor curMain = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (curMain.getCount() > 0) {
while (curMain.moveToNext()) {
String contactId = curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts._ID));
displayName = curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{contactId}, null);
contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { contactId }, null);
Cursor emailCursor = contentResolver.query(Email.CONTENT_URI, null,Email.CONTACT_ID + " = " + contactId, null, null);
if(emailCursor.getCount()>0){
while (emailCursor.moveToNext()) {
email = emailCursor.getString(emailCursor.getColumnIndex(Email.DATA));
}
}else{
email = "";
}
emailCursor.close();
if(phoneCursor.getCount()>0){
while (phoneCursor.moveToNext()) {
phoneNo = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}else{
phoneNo = "";
}
phoneCursor.close();
ContactModel cm = new ContactModel();
cm.setDisplayName(displayName);
cm.setPhoneNumber(phoneNo);
cm.setEmail(email);
list.add(cm);
}
}
}
curMain.close();
return list;
}
public static class ContactModel implements Comparable<ContactModel>{
private String displayName = "", phoneNumber = "", email = "";
@Override
public int compareTo(ContactModel another) {
return getDisplayName().compareTo(another.getDisplayName());
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public ContactModel() {
}
public ContactModel(String name) {
this.displayName = name;
}
public String toString() {
return displayName;
}
}
}
在清单文件中添加权限:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
希望它会对你有所帮助。