簡體   English   中英

如何從Android中的聯系人應用程序獲取唯一的聯系號碼?

[英]How to get unique contact number from my contact application in android?

我正在聯系申請,但當我得到所有聯系號碼時,我會得到重復的號碼。 我怎樣才能確保我只獲得唯一的數字?

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cur != null && cur.getCount() > 0) {
            while (cur.moveToNext()) {
                strPhontNumberTemp = "";
                mPhoneContactsVo = new PhoneContactsVo();

                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) {

                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = ?", new String[] { id }, null);

                     while (pCur.moveToNext()) {
                         String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Log.i(TAG, "phoneNumber="+phoneNumber); // Dupblicate number print
                     }
                }
            }
        }

使用“ Set界面”添加電話號碼以避免重復。

Set<String> uniques = new HashSet<String>();

檢查這個簡單的例子

   public static void main(String[] args) {
    Set<String> uniques = new HashSet<String>();
    Set<String> dups    = new HashSet<String>();

    for (String a : args)
        if (!uniques.add(a))
            dups.add(a);

    // Destructive set-difference
    uniques.removeAll(dups);

    System.out.println("Unique words:    " + uniques);
    System.out.println("Duplicate words: " + dups);
  }

從這個鏈接... http://docs.oracle.com/javase/tutorial/collections/interfaces/set.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM