简体   繁体   English

如何从Android中的联系人应用程序获取唯一的联系号码?

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

I am making a contact application but when I get all contact numbers I get duplicate numbers. 我正在联系申请,但当我得到所有联系号码时,我会得到重复的号码。 How can I make sure that I only get unique numbers? 我怎样才能确保我只获得唯一的数字?

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

Use Set Interface to add the phone numbers to avoid duplicate. 使用“ Set界面”添加电话号码以避免重复。

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

Check this simple example 检查这个简单的例子

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

from this link ... http://docs.oracle.com/javase/tutorial/collections/interfaces/set.html 从这个链接... http://docs.oracle.com/javase/tutorial/collections/interfaces/set.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM