6

से अद्वितीय संपर्कों का चयन कैसे करें मैं अद्वितीय संपर्क केवल उन संपर्कों को चुनना चाहता हूं जिनके पास फ़ोन नंबर हैं। मैं इस कोड का उपयोग कर रहा हूंएंड्रॉइड

ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
       null, null, ContactsContract.Contacts.DISPLAY_NAME); 
     // Find the ListView resource. 
     mainListView = (ListView) findViewById(R.id.mainListView); 

     // When item is tapped, toggle checked properties of CheckBox and 
     // Planet. 
     mainListView 
       .setOnItemClickListener(new AdapterView.OnItemClickListener() 
       { 
        public void onItemClick(AdapterView<?> parent, View item, 
          int position, long id) 
        { 
         ContactsList planet = listAdapter.getItem(position); 
         planet.toggleChecked(); 
         PlanetViewHolder viewHolder = (PlanetViewHolder) item 
           .getTag(); 
         viewHolder.getCheckBox().setChecked(planet.isChecked()); 
        } 
       }); 

     // Create and populate planets. 
     planets = (ContactsList[]) getLastNonConfigurationInstance(); 
     // planets = new Planet[10]; 
     // planets.Add("asdf"); 
     ArrayList<ContactsList> planetList = new ArrayList<ContactsList>(); 
     String phoneNumber = null; 
     String phoneType = null; 

     count = cur.getCount(); 
     contacts = new ContactsList[count]; 

     if (planets == null) 
     { 
      if (cur.getCount() > 0) 
      { 
       planets = new ContactsList[cur.getCount()]; 
       int i = 0; 
       // 
       while (cur.moveToNext()) 
       { 
        String id = cur.getString(cur 
          .getColumnIndex(ContactsContract.Contacts._ID)); 
        String name = cur 
          .getString(cur 
            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        if (Integer 
          .parseInt(cur.getString(cur 
            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
        { 
         // Query phone here. Covered next 
         Cursor pCur = cr 
           .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
             null, 
             ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
               + " = ?", new String[] 
             { id }, null); 

         // WHILE WE HAVE CURSOR GET THE PHONE NUMERS 
         while (pCur.moveToNext()) 
         { 
          // Do something with phones 
          phoneNumber = pCur 
            .getString(pCur 
              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); 

          phoneType = pCur 
            .getString(pCur 
              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 

          Log.i("Pratik", name + "'s PHONE :" + phoneNumber); 
          Log.i("Pratik", "PHONE TYPE :" + phoneType); 
         } 
         pCur.close(); 
        } 

        planets = new ContactsList[] 
        { new ContactsList(name, phoneNumber) }; 

        contacts[i] = planets[0]; 
        planetList.addAll(Arrays.asList(planets)); 

        i++; 
       } 
      } 

यह कोड सभी संपर्कों को पुनर्प्राप्त करता है और एक सूची में डाल देता है। लेकिन मुझे अद्वितीय संपर्क चाहिए और केवल वही है जिसमें फोन नंबर है। मैं यह कैसे कर सकता हूँ?? केवल अनन्य संपर्कों का चयन करने के लिए क्वेरी में कुछ तर्क पारित करने के लिए कोई तरीका है ???

उत्तर

11

मुझे लगता है कि आप का मतलब है कि आप कुछ संपर्कों के लिए रिकॉर्ड नकल मिल पाने के लिए। तो आपको अपनी क्वेरी के लिए शर्त जोड़नी होगी। आवश्यक हिस्सा संपर्क समूह दृश्य समूह और में फ़ोन नंबर होना चाहिए।

String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" 
       + ("1") + "'"; 
     String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
       + " COLLATE LOCALIZED ASC"; 
cur = context.getContentResolver().query(
       ContactsContract.Contacts.CONTENT_URI, projection, selection 
         + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER 
         + "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated 
+0

पूर्व आईसीएस पर काम नहीं कर रहा है ... –

+0

@Puru: मैंने इसे एंड्रॉइड 2.2 –

+0

@Jul पर उपयोग किया: क्या आईसीएस और उपरोक्त संस्करणों के लिए कोई समाधान है? –

0

आसान तरीका PHONENUMBERS और संपर्क नाम

// set as global 
Set<string> phonenumbersList = new HashSet<string>(); 

      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
      while (phones.moveToNext()) 
      { 
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

      //contact has name number and phonenumber does not exists in list 
      if (phoneNumber != null && name != null && !phonenumbersList.contains(phoneNumber)){ 
       planets = new ContactsList[]{ new ContactsList(name, phoneNumber) }; 

       phonenumbersList.add(phoneNumber); 
       planetList.addAll(Arrays.asList(planets)); 
       planetList.Add(phoneNumber, name); 
      } 
      } 
      phones.close(); 
+1

मुझे पहले से ही मेरे कोड के साथ फोननंबर मिल रहा है। मैं अद्वितीय संपर्क –

+2

चुनना चाहता हूं और आप अद्वितीय के तहत क्या समझते हैं? – user1841306

+0

मुझे पुनरावृत्ति नहीं चाहिए। एक फोन नंबर केवल एक बार –

1

यह मेरे लिए फोन नंबर से संपर्क करने के लिए काम कर रहा है। यहां हम डेटा तालिका से पूछताछ कर रहे हैं, और CONTACT_ID contact provider documentation

@Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 

final String ORDER_BY = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " ASC"; 

    final String[] PROJECTION = { 
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID, 
      ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY, 
      ContactsContract.CommonDataKinds.Phone.NUMBER 
    }; 

return new CursorLoader(
       context, 
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       PROJECTION, 
       null, 
       null, 
       ORDER_BY 
     ); 
}