2012-03-10 23 views
8

Tại đây, tên Liên hệ của tôi được hiển thị trên Chế độ xem danh sách. Bằng cách nhấp vào Danh sách tôi nhận được ContactNameContact Id. Từ đó tôi muốn tìm nạp Phone number bằng cách sử dụng Contact ID hoặc Contact name, hãy giúp tôi.Cách nhận Số liên lạc cụ thể bằng cách sử dụng Id liên hệ

Đây là Mã My

void ReadContacts(String sort) { 
    final Uri uri = ContactsContract.Contacts.CONTENT_URI; 
    final String[] projection = new String[] { 
      ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME 
    }; 
    //boolean mShowInvisible = false; 
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'"; 
    String[] selectionArgs = null; 
    final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

    m_curContacts = managedQuery(uri, projection, selection, selectionArgs, sortOrder); 
    String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME}; 
    m_slvAdapter = new SimpleCursorAdapter(this, 
       android.R.layout.simple_list_item_1, 
       m_curContacts, 
       fields, 
       new int[] {android.R.id.text1}); 
    m_slvAdapter.setFilterQueryProvider(new FilterQueryProvider() { 

     public Cursor runQuery(CharSequence constraint) { 
      Log.d(LOG_TAG, "runQuery constraint:"+constraint); 
      String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'" + 
        " AND "+ ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%"+constraint+"%'"; 
      String[] selectionArgs = null;//new String[]{"'1'"};//, }; 
      Cursor cur = managedQuery(uri, projection, selection, selectionArgs, sortOrder); 
      return cur; 
     } 

    }); 
    m_lvContacts.setAdapter(m_slvAdapter); 
    // cur.close(); 

} 



public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { 
    ContentResolver cr; 
    Cursor cursor = (Cursor) m_lvContacts.getItemAtPosition(position); 
    String szDisplayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
    String szId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 
    int nId = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 
    Log.d(LOG_TAG, "Item click:"+position+" szId:"+szId+" nId:"+nId+" Data:"+szDisplayName); 
    Toast.makeText(getBaseContext(), "Item click:"+phoneNumber+" szId:"+szId+" nId:"+nId+" Data:"+szDisplayName, Toast.LENGTH_SHORT).show(); 

} 
+0

xem tại đây http://stackoverflow.com/questions/3370628/retrieve-contact-phone-number-from-uri-in-android –

+0

tôi đã sử dụng liên kết ở trên nhưng phoneindex tạo ra giá trị o – user933909

+0

http://stackoverflow.com /questions/3044545/get-contact-info-from-android-contact-picker.Vui lòng thử tìm kiếm trên SO.Những câu hỏi đã hỏi và bạn phải sắp xếp nó –

Trả lời

18

Hãy thử điều này một

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); 
+0

tôi không muốn danh sách điện thoại số tôi cần một số cụ thể – user933909

+0

Bạn đã kiểm tra? Tôi nghĩ Nó chỉ trả về một con số – Dhaval

0

Bạn sẽ tìm thấy PHONENUMBERS trong bảng dữ liệu. Tìm id liên hệ thô tương ứng trong bảng rawcontacts (sử dụng ID liên hệ làm tiêu chí), sử dụng các id liên hệ thô này để truy vấn bảng dữ liệu. Các phonenumber sẽ có một loại mime nhất định (tránh các tài liệu ngay bây giờ) để bạn có thể nhận ra chúng. Hy vọng điều này sẽ hữu ích!

Mẹo: sao chép cơ sở dữ liệu contacts2.db từ trình giả lập/thiết bị của bạn và sử dụng Squirrel (trình điều khiển jdbc SQLite) để poke xung quanh trong cơ sở dữ liệu danh bạ. Db nằm dưới/data/data/look for contacts trong danh sách).

+0

vui lòng cung cấp một số mã – user933909

0

Guys Tôi nhận được trả lời:

Hãy Sử dụng Bộ luật này nếu bạn cần:

Cursor cursor = (Cursor) m_lvContacts.getItemAtPosition(position); 
     String szDisplayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
     String szId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 
     int nId = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 
     long l = Long.parseLong(szId); 
     int type=ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE; 




cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
     null, 
     ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"='"+szId+"'", 
     null, null); 

      int phoneNumberIndex = cursor 
        .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER); 

      Log.d(TAG, String.valueOf(cursor.getCount())); 

      if (cursor != null) { 
       Log.v(TAG, "Cursor Not null"); 
       try { 
        if (cursor.moveToNext()) { 
         Log.v(TAG, "Moved to first"); 
         Log.v(TAG, "Cursor Moved to first and checking"); 
         phoneNumber = cursor.getString(phoneNumberIndex); 
         System.out.println("HAGSd"+phoneNumber); 
        } 
       } finally { 
        Log.v(TAG, "In finally"); 
        cursor.close(); 
       } 
0
public static final String contactIdByPhoneNumber(Context ctx, String phoneNumber) { 
    String contactId = null; 
    if (phoneNumber != null && phoneNumber.length() > 0) { 
     ContentResolver contentResolver = ctx.getContentResolver(); 

     Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 

     String[] projection = new String[] { PhoneLookup._ID }; 

     Cursor cursor = contentResolver.query(uri, projection, null, null, null); 

     if (cursor != null) { 
      while (cursor.moveToNext()) { 
       contactId = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID)); 
      } 
      cursor.close(); 
     } 
    } 
    return contactId; 
} 
2

Hãy thử điều này một

public void getNameUsingContactId(String contactId){ 

    String cContactIdString = ContactsContract.Contacts._ID; 
    Uri cCONTACT_CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 
    String cDisplayNameColumn = ContactsContract.Contacts.DISPLAY_NAME; 

    String selection = cContactIdString + " = ? "; 
    String[] selectionArgs = new String[]{String.valueOf(contactId)}; 

    Cursor cursor = mContext.getContentResolver().query(cCONTACT_CONTENT_URI, null, selection, selectionArgs, null); 
    if ((cursor != null) && (cursor.getCount() > 0)) { 
     cursor.moveToFirst(); 
     while ((cursor != null) && (cursor.isAfterLast() == false)) { 
      if (cursor.getColumnIndex(cContactIdString) >= 0) { 
       if (contactId.equals(cursor.getString(cursor.getColumnIndex(cContactIdString)))) { 
        String name = cursor.getString(cursor.getColumnIndex(cDisplayNameColumn)); 
        break; 
       } 
      } 
      cursor.moveToNext(); 
     } 
    } 
    if (cursor != null) 
     cursor.close(); 
} 
Các vấn đề liên quan