2012-03-08 36 views
8

Tôi đang cố gắng tìm một số liên lạc theo tên hiển thị. Mục đích là để mở liên hệ này và thêm nhiều dữ liệu hơn cho nó (cụ thể là số điện thoại), nhưng tôi đang đấu tranh để tìm thấy số liên lạc mà tôi muốn cập nhật.Android - Tìm số liên lạc theo tên hiển thị

Đây là mã Tôi đang sử dụng:

public static String findContact(Context context) { 

    ContentResolver contentResolver = context.getContentResolver(); 
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI; 
    String[] projection = new String[] { PhoneLookup._ID }; 
    String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ?"; 
    String[] selectionArguments = { "John Johnson" }; 
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null); 

    if (cursor != null) { 
     while (cursor.moveToNext()) { 
      return cursor.getString(0); 
     } 
    } 
    return "John Johnson not found"; 
} 

tôi có một số liên lạc gọi là "John Johnson", nhưng phương pháp này luôn trả về "không tìm thấy". Tôi cũng đã thử tìm kiếm một liên hệ chỉ với một tên, do đó không có sự khác biệt.

Tôi nghi ngờ rằng có vấn đề gì đó, vì tôi không thể tìm thấy ví dụ trực tuyến để tìm kiếm địa chỉ liên hệ với tên hiển thị cụ thể, và dường như tên hiển thị là một loại thông tin đặc biệt, khác với ví dụ số điện thoại.

Bất kỳ ý tưởng nào tôi có thể đạt được để tìm John Johnson?


UPDATE: Tôi phát hiện ra làm thế nào để tìm thấy một số liên lạc bằng tên hiển thị:

 ContentResolver contentResolver = context.getContentResolver(); 
    Uri uri = Data.CONTENT_URI; 
    String[] projection = new String[] { PhoneLookup._ID }; 
    String selection = StructuredName.DISPLAY_NAME + " = ?"; 
    String[] selectionArguments = { "John Johnson" }; 
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null); 

    if (cursor != null) { 
     while (cursor.moveToNext()) { 
      return cursor.getString(0); 
     } 
    } 
    return "John Johnson not found"; 

Mã này trả về id xúc của tiếp xúc đầu tiên với tên hiển thị "John Johnson". Trong mã ban đầu của tôi tôi đã có sai uri và lựa chọn sai trong truy vấn của tôi.

Trả lời

1

Tôi nghĩ rằng sự cố có thể do dự đoán bạn đặt. Chiếu được sử dụng để cho android biết cột dữ liệu nào bạn muốn truy vấn, sau đó bạn chỉ cung cấp cột id để tên hiển thị không trả lại. Hãy thử loại bỏ phép chiếu để xem nó có hoạt động hay không.

-- Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);
++ Cursor cursor = contentResolver.query(uri, null, selection, selectionArguments, null);

+1

Cảm ơn bạn đã trả lời, nhưng nó đã không giúp đỡ để có chiếu null. Cùng một mã hoạt động khi tôi đang tìm kiếm một số điện thoại, vì vậy có thể có PhoneLookup._ID làm phép chiếu ngay cả khi tôi đang tìm kiếm một kết quả phù hợp trong một cột khác. Nếu tôi đã hiểu chính xác nó, phép chiếu là dữ liệu bạn muốn lấy lại từ truy vấn, không phải dữ liệu bạn đang tìm kiếm. Vì vậy, nếu bạn đặt phép chiếu thành null, bạn chỉ cần yêu cầu nhận tất cả dữ liệu từ các liên hệ phù hợp mà bạn nhận được từ truy vấn của mình. –

0
  //method for gaining id 
//this method get a name and make fetch it's id and then send the id to other method //named "showinformation" and that method print information of that contact 
     public void id_return(String name) { 
       String id_name=null; 
       Uri resultUri = ContactsContract.Contacts.CONTENT_URI; 
       Cursor cont = getContentResolver().query(resultUri, null, null, null, null); 
       String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " = ?" ; 
       String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,name}; 
       Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
       while (nameCur.moveToNext()) { 
       id_name = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID));} 
       nameCur.close(); 
       cont.close(); 
       nameCur.close(); 
//for calling of following method 
       showinformation(id_name); 
      } 

      //method for showing information like name ,phone, email and other thing you want 
      public void showinformation(String id) { 
       String name=null; 
       String phone=null; 
       String email=null; 
       Uri resultUri = ContactsContract.Contacts.CONTENT_URI; 
       Cursor cont = getContentResolver().query(resultUri, null, null, null, null); 
       String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID+ " = ?" ; 

       String[] whereNameParams1 = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,id}; 
       Cursor nameCur1 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams1, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
       while (nameCur1.moveToNext()) { 
       name = nameCur1.getString(nameCur1.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));} 
       nameCur1.close(); 
       cont.close(); 
       nameCur1.close(); 


       String[] whereNameParams2 = new String[] { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,id}; 
       Cursor nameCur2 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams2, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
       while (nameCur2.moveToNext()) { 
       phone = nameCur2.getString(nameCur2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));} 
       nameCur2.close(); 
       cont.close(); 
       nameCur2.close(); 


       String[] whereNameParams3 = new String[] { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,id}; 
       Cursor nameCur3 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams3, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
       while (nameCur3.moveToNext()) { 
       email = nameCur3.getString(nameCur3.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));} 
       nameCur3.close(); 
       cont.close(); 
       nameCur3.close(); 

       String[] whereNameParams4 = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,id}; 
       Cursor nameCur4 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams4, ContactsContract.CommonDataKinds.StructuredPostal.DATA); 
       while (nameCur4.moveToNext()) { 
       phone = nameCur4.getString(nameCur4.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA));} 
       nameCur4.close(); 
       cont.close(); 
       nameCur4.close(); 
    //showing result 
      txadd.setText("Name= "+ name+"\nPhone= "+phone+"\nEmail= "+email); 


      } 

//thank all persons in this site because of many help of me to learn and correction my warn and errors this is only a gift for all of you and ... 
0

Mã dưới đây sẽ làm các trick

if (displayName != null) { 
     Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(displayName)); 
     String[] displayNameProjection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME }; 
     Cursor cur = context.getContentResolver().query(lookupUri, displayNameProjection, null, null, null); 
     try { 
      if (cur.moveToFirst()) { 
       return true; 
      } 
     } finally { 
      if (cur != null) 
       cur.close(); 
     } 
     return false; 
    } else { 
     return false; 
    } 

tham khảo: Retrieving a List of Contacts Article

Các vấn đề liên quan