2012-03-08 19 views
8

मैं प्रदर्शन नाम से संपर्क ढूंढने की कोशिश कर रहा हूं। लक्ष्य इस संपर्क को खोलना है और इसमें अधिक डेटा जोड़ना है (विशेष रूप से अधिक फोन नंबर), लेकिन मैं जिस संपर्क को अपडेट करना चाहता हूं उसे ढूंढने के लिए भी संघर्ष कर रहा हूं।एंड्रॉइड - प्रदर्शन नाम से संपर्क खोजें

इस कोड मैं का उपयोग कर रहा है:

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

मैं एक संपर्क "जॉन जॉनसन" कहा जाता है, लेकिन विधि हमेशा रिटर्न "नहीं मिला"। मैंने केवल एक ही नाम से संपर्क खोजने की कोशिश की, जिससे कोई फर्क नहीं पड़ता।

मुझे संदेह है कि यह यूरी, चयन या चयन तर्कों में कुछ गड़बड़ है, क्योंकि मुझे किसी दिए गए डिस्प्ले नाम के साथ संपर्कों की खोज करने के लिए ऑनलाइन कोई उदाहरण नहीं मिला, और ऐसा लगता है कि डिस्प्ले नाम एक विशेष प्रकार की जानकारी है, उदाहरण के लिए एक फोन नंबर से अलग।

जॉन जॉनसन को खोजने के लिए मैं कोई विचार कैसे प्राप्त कर सकता हूं?


अद्यतन: मुझे पता चला कैसे प्रदर्शन नाम से एक संपर्क को खोजने के लिए:

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

इस कोड को प्रदर्शन नाम "जॉन जॉनसन" से पहले संपर्क के संपर्क आईडी देता है। मेरे मूल कोड में मेरे पास गलत क्वेरी और मेरी क्वेरी में गलत चयन था।

उत्तर

1

मुझे लगता है कि समस्या आपके द्वारा निर्धारित प्रक्षेपण के कारण हो सकती है। प्रक्षेपण का उपयोग एंड्रॉइड को बताने के लिए किया जाता है कि आप किस डेटा के कॉलम को क्वेरी करना चाहते हैं, फिर आप केवल आईडी कॉलम दें ताकि डिस्प्ले नाम वापस नहीं आएगा। यह देखने के लिए प्रक्षेपण को हटाने का प्रयास करें कि यह काम करता है या नहीं।

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

+1

जवाब देने के लिए धन्यवाद, लेकिन यह शून्य प्रक्षेपण के लिए मदद नहीं की। जब मैं फ़ोन नंबर खोज रहा हूं, वही कोड काम करता है, इसलिए यदि मैं किसी अन्य कॉलम में एक मैच खोज रहा हूं, तो भी फोन लुकअप._आईडी प्रक्षेपण के रूप में संभव है। यदि मैं इसे सही ढंग से समझ गया हूं, प्रक्षेपण वह डेटा है जिसे आप क्वेरी से वापस प्राप्त करना चाहते हैं, न कि आप जो खोज रहे हैं। इसलिए यदि आप शून्य पर प्रक्षेपण सेट करते हैं, तो आप बस अपनी क्वेरी से मिलने वाले मिलान करने वाले संपर्कों से सभी डेटा प्राप्त करने के लिए कह रहे हैं। –

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

नीचे कोड चाल

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

संदर्भ करना चाहिए: Retrieving a List of Contacts Article

संबंधित मुद्दे