2011-01-18 2 views
2

내가 그들의 contactId의로 연락처의 전화 번호를 조회하려고 시도하고, BLL 결과를 반환하는 - 전화 번호 : 나는 SO 주변의 다른 예를 사용하지만 해봤 1ContactsContract 조회 전화 번호

내가 점점 계속 커서

Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, contactId); 

Log.i(TAG, "Locate Contact by Id: " + contactId + " at Uri: " + uri.toString()); 

Cursor cursor = this.getContentResolver().query(uri, null, null, null, null); 

try { 
    if (cursor.moveToFirst()) { 
     Log.i(TAG, "Phone Number: " + cursor.getString(cursor.getColumnIndex(Phone.NUMBER))); 
    } 
} finally { 
    cursor.close(); 
} 

답변

2

i를 Phone.LOOKUP_KEY보다는 Phone.CONTACT_ID에 의해 그 일을 결국;

private HashMap<String, CharSequence> lookupPhoneNumbers(String lookupKey) 
{ 
    HashMap<String, CharSequence> numbers = new HashMap<String, CharSequence>(); 

    Cursor cursor = getContext().getContentResolver().query(Phone.CONTENT_URI, null, Phone.LOOKUP_KEY + " = ?", new String[] { lookupKey }, null); 
    try 
    { 
     while (cursor.moveToNext()) 
     { 
      String phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER)); 
      int type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE)); 
      CharSequence phoneLabel = Phone.getTypeLabel(getResources(), type, "Undefined"); 
      // boolean isPrimary = (cursor.getInt(cursor.getColumnIndex(Phone.IS_PRIMARY)) == 1); 

      numbers.put(phoneNumber, phoneLabel); 
     } 
    } finally 
    { 
     cursor.close(); 
    } 

    return numbers; 
} 
+2

사용하신 조회 키는 무엇입니까? –

6

에 0 카운트가 이것을 시도 :

private ArrayList<String> getPhoneNumbers(String id) 
{ 
    ArrayList<String> phones = new ArrayList<String>(); 

    Cursor cursor = mContentResolver.query(
      CommonDataKinds.Phone.CONTENT_URI, 
      null, 
      CommonDataKinds.Phone.CONTACT_ID +" = ?", 
      new String[]{id}, null); 

    while (cursor.moveToNext()) 
    { 
     phones.add(cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER))); 
    } 

    cursor.close(); 
    return(phones); 
}