2017-10-09 117 views
0

我在寫一個應用程序,以顯示在ListView聯繫人號碼,但它給重複的聯繫人條目我想只顯示一個聯繫人單獨重複的聯繫人不顯示在listiview

private void getContactList() { 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 

    if ((cur != null ? cur.getCount() : 0) > 0) { 
     while (cur != null && cur.moveToNext()) { 
      String id = cur.getString(
        cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(
        ContactsContract.Contacts.DISPLAY_NAME)); 

      if (cur.getInt(cur.getColumnIndex(
        ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { 
       Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
         new String[]{id}, null); 
       while (pCur.moveToNext()) { 
        String phoneNo = pCur.getString(pCur.getColumnIndex(
          ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        ContactsPlacer obj = new ContactsPlacer(); 
        obj.setContactname(name); 
        obj.setContactnumber(phoneNo); 
        names.add(obj); 

       } 
       pCur.close(); 
      } 
     } 
    } 
    if(cur!=null){ 
     cur.close(); 
    } 




} 

幫我一下的是,重複的聯繫人列表視圖中不顯示

+0

每個聯繫人可以有多個電話號碼,電子郵件地址等 –

+0

相同的數據顯示 –

+0

同名手機號碼 –

回答

0
boolean checkexsistance(ContactsPlacer obj) 
{ 
boolean test= true; 
for(int i = 0 ; i<names.size();i++) 
{ 
if(names.get(i).getContactname().equals(obj.getContactname())) 
{ 
    test = false; 
    break; 

} 

} 


return test; 
} 



private void getContactList() { 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 

    if ((cur != null ? cur.getCount() : 0) > 0) { 
     while (cur != null && cur.moveToNext()) { 
      String id = cur.getString(
        cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(
        ContactsContract.Contacts.DISPLAY_NAME)); 

      if (cur.getInt(cur.getColumnIndex(
        ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { 
       Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
         new String[]{id}, null); 
       while (pCur.moveToNext()) { 
        String phoneNo = pCur.getString(pCur.getColumnIndex(
          ContactsContract.CommonDataKinds.Phone.NUMBER)); 


        ContactsPlacer obj = new ContactsPlacer(); 
        obj.setContactname(name); 
        obj.setContactnumber(phoneNo); 
        if(checkexsistance(obj)) 
         names.add(obj); 

       } 
       pCur.close(); 
      } 
     } 
    } 
    if(cur!=null){ 
     cur.close(); 
    } 




} 
+0

那是我想要的東西 –

0

試試這個

if(email!=null){ 
    if(!names.contains(name)){ 
     names.add(name); 
    } 
} 
0

在這裏使用ArrayList中的方法包含這樣

public ArrayList<String> getNameEmailDetails(){ 
    ArrayList<String> names = new ArrayList<String>(); 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) { 
      String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      Cursor cur1 = cr.query(
        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
        new String[]{id}, null); 
      while (cur1.moveToNext()) { 
       //to get the contact names 
       String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       Log.e(TAG+"Name :", name); 
       String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
       Log.e(TAG+"Email", email); 
       if(email!=null){ 
        if(!names.contains(name)) 
        names.add(name); 
       } 
      } 
      cur1.close(); 
     } 
    } 
    return names; 
} 
0

我有同樣的問題,前幾天,並使用此代碼解決一樣, 請給它一個嘗試,讓我知道,如果再需要幫助。

不要忘記索要許可,並在清單 「android.permission.READ_CONTACTS」提

ContentResolver cr = getActivity().getContentResolver(); 
     String[] PROJECTION = new String[]{ContactsContract.RawContacts._ID, 
       ContactsContract.Contacts.DISPLAY_NAME, 
       ContactsContract.Contacts.PHOTO_ID, 
       ContactsContract.CommonDataKinds.Email.DATA, 
       ContactsContract.CommonDataKinds.Phone.NUMBER, 
       ContactsContract.RawContacts.VERSION}; 
     String order = "CASE WHEN " 
       + ContactsContract.Contacts.DISPLAY_NAME 
       + " NOT LIKE '%@%' THEN 1 ELSE 2 END, " 
       + ContactsContract.Contacts.DISPLAY_NAME 
       + ", " 
       + ContactsContract.CommonDataKinds.Phone.DATA 
       + " COLLATE NOCASE"; 
     String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + ContactsContract.CommonDataKinds.Phone.TYPE +"=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE; 
     Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, filter, null, order); 

     for (int i = 0; i < cur.getColumnCount(); i++) { 
      Timber.e("column " + i + "=" + cur.getColumnName(i)); 
     } 

     if (cur.moveToFirst()) { 
      do { 
       String name = cur.getString(1); 
       String number = cur.getString(4); 

       number = Utils.removeExtraCharFromString(number); 

       if (number.length() > 8) { 
        mContactNumbers = mContactNumbers + number + ","; 
        PhoneContacts phoneContacts = new PhoneContacts(); 
        try { 
         phoneContacts.setName(URLEncoder.encode(name, "UTF-8")); 
         phoneContacts.setNumber(number); 
         phoneContacts.setNameInitial(String.valueOf(phoneContacts.getName().charAt(0))); 

         listOfContactsRaw.add(phoneContacts); 
        } catch (UnsupportedEncodingException e) { 
         e.printStackTrace(); 
        } 
       } 
      } while (cur.moveToNext()); 
     } 
     cur.close(); 

     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       Set set = new TreeSet(new Comparator() { 
        @Override 
        public int compare(Object o1, Object o2) { 

         PhoneContacts phoneContacts1 = (PhoneContacts) o1; 
         PhoneContacts phoneContacts2 = (PhoneContacts) o2; 
         if (phoneContacts1.getNumber().equalsIgnoreCase(phoneContacts2.getNumber())) { 
          return 0; 
         } 
         return 1; 
        } 
       }); 
       set.addAll(listOfContactsRaw); 

       System.out.println("\n***** After removing duplicates *******\n"); 

       listOfContacts = new ArrayList(set); 

       // Set listOfContacts with your recycler view 
      } 
     }, 200); 
相關問題