2015-07-13 38 views
0

鄉親們!Android:如何通過聯繫人_ID聯繫DisplayName?

看我打破了我的想法 - 如何通過_ID從ContactContract中獲取display_name?


下面是代碼:

public static long getId(Context context, String number) { 
    long id = 0; 
    String displayName; 
    // define the columns I want the query to return 
    final String[] projection = new String[] { 
      ContactsContract.PhoneLookup.DISPLAY_NAME, 
      ContactsContract.PhoneLookup._ID}; 
    // encode the phone number and build the filter URI 
    final Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 
    // query time 
    final Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null); 
    if(cursor != null) { 
     if (cursor.moveToFirst()) { 
      displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 

      // HERE IS GOOD _ID! 
      id = cursor.getLong(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID)); 
      Log.d("_TAG_", displayName); 

      { 
       // 
       // v. N-1 
       // 
       Cursor cursor2 = context.getContentResolver().query(
         contactUri, 
         projection, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
         new String[]{String.valueOf(id)}, null); 

       if(cursor2 != null) { 
        // Cursor valid but string below got error "android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 :)" 
        displayName = cursor2.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
        Log.d("_TAG_", displayName); 
       } 
      } 

      { 
       // 
       // v. N 
       // 
       Uri myPhoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 
         Uri.encode(String.valueOf(id))); 
       Cursor phoneCursor = context.getContentResolver().query(
         myPhoneUri, null, null, null, null); 

       for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) { 
        // No steps inside! 
        String str = phoneCursor.getString(phoneCursor 
          .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Nickname.NAME)); 

        Log.d(App.LOG_TAG, "ContactUtils.getId: " + str); 

       } 

       if(phoneCursor != null) { 
        phoneCursor.close(); 
       } 
      } 


     } else { 
      Log.d("_TAG_", "Contact Not Found @ " + number); 
     } 
     cursor.close(); 
    } 
    return id; 
} 

「v.N-1」 和 「v.N」 從之前一百萬的嘗試只有2人。

請參閱 - 我得到了_ID,那麼爲什麼它不作爲有效的查詢工作?

回答

0

這是我發現的決定。

public static String getDisplayName(Context context, long id) { 
    String displayName = null; 
    // define the columns I want the query to return 
    final String[] projection = new String[] { 
      ContactsContract.Contacts.DISPLAY_NAME, 
    }; 
    final Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null); 
    if(cursor != null) { 
     if (cursor.moveToFirst()) { 
      displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     } 
     cursor.close(); 
    } 
    return displayName; 
} 

它的工作原理...

相關問題