我正在尝试阅读用户联系人列表中的联系号码。这是我的代码
Cursor cursor = getContacts();
if(cursor.getCount()>0){
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
int numberField = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
textViewDisplay.append("Name: ");
textViewDisplay.append(displayName+"Number :"+numberField);
textViewDisplay.append("\n");
}
}
答案 0 :(得分:2)
您正在cursor.getColumnIndex(COLUMN)
进入int
。所以它所说的方法返回发送给它的COLUMN
索引作为参数。您需要int
永远不会包含的电话号码,因为其大小始终大于4个字节,并且还包含一些特殊字符,如+
因此,您需要将Number
带到某个String
变量
使用String numberField = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
答案 1 :(得分:1)
使用此
String numberField = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
而不是
int numberField = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
答案 2 :(得分:1)
返回“1”表示联系人有电话号码。试试这个,
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String cNumber="";
if (hasPhone.equalsIgnoreCase("1"))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phones.close();
}
答案 3 :(得分:1)
试试这个:
public void readContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// Get contact id (id)
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// Get contact name (displayName)
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Get Phone Number....
Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?";
String[] SELECTION_ARRAY_PHONE = new String[] { id };
Cursor currPhone = cr.query(URI_PHONE, null,SELECTION_PHONE, SELECTION_ARRAY_PHONE, null);
int indexPhoneNo = currPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int indexPhoneType = currPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
if (currPhone.getCount() > 0) {
while (currPhone.moveToNext()) {
String phoneNoStr = currPhone.getString(indexPhoneNo);
String phoneTypeStr = currPhone.getString(indexPhoneType);
}
}
currPhone.close();
}
}
}
cur.close();
}