2012-03-08 6 views
8

Próbuję znaleźć kontakt według wyświetlanej nazwy. Celem jest otwarcie tego kontaktu i dodanie do niego więcej danych (w szczególności więcej numerów telefonów), ale staram się znaleźć kontakt, który chcę zaktualizować.Android - Znajdź kontakt według wyświetlanej nazwy

Jest to kod używam:

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"; 
} 

mam kontaktu o nazwie „John Johnson”, ale metoda zawsze zwraca „nie znaleziono”. Próbowałem także wyszukać kontakt z tylko jedną nazwą, więc nie ma znaczenia.

Podejrzewam, że jest coś nie tak z argumentami o uri, selekcji lub selekcji, ponieważ nie mogłem znaleźć żadnego przykładu wyszukiwania kontaktów z daną nazwą wyświetlaną i wydaje się, że nazwa wyświetlana jest specjalnym rodzajem informacji, różni się na przykład od numeru telefonu.

Jakieś pomysły, w jaki sposób mogę znaleźć Johna Johnsona?


UPDATE: Znalazłem się, jak znaleźć kontakt według nazwy wyświetlacza:

 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"; 

Ten kod zwraca id styku pierwszego kontaktu z wyświetlanej nazwy „John Johnson”. W moim oryginalnym kodzie miałem błędne wybranie URI i niewłaściwą opcję w moim zapytaniu.

Odpowiedz

1

Uważam, że problem mógł być spowodowany przez ustawioną projekcję. Projekcja służy do informowania Androida, która kolumna danych chcesz zapytać, a następnie podajesz tylko kolumnę id, więc wyświetlana nazwa nie zwróci. Spróbuj usunąć projekcję, aby zobaczyć, czy działa.

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

+1

Dzięki za odpowiedzi, ale to nie pomogło mieć zerową projekcję. Ten sam kod działa, gdy szukam numeru telefonu, więc możliwe jest posiadanie PhoneLookup._ID jako projekcji, nawet jeśli szukam dopasowania w innej kolumnie. Jeśli dobrze zrozumiałem, projekcja to dane, które chcesz odzyskać z zapytania, a nie to, czego szukasz. Jeśli więc ustawisz projekcję na null, po prostu prosisz o uzyskanie wszystkich danych z pasujących kontaktów, które otrzymujesz z zapytania. –

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

Poniższy kod powinien rade

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; 
    } 

referencyjny: Retrieving a List of Contacts Article

Powiązane problemy