本文介绍了如何从Android中我接触的应用程序中获取独特的联系方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个接触的应用程序,但是当我得到的所有联系电话,我得到重复的数字。我怎样才能确保我只得到唯一的数字?

  ContentResolver的CR = getContentResolver();
        光标CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
        如果(CUR = NULL&放大器;!&安培; cur.getCount()0){
            而(cur.moveToNext()){
                strPhontNumberTemp =;
                mPhoneContactsVo =新PhoneContactsVo();                字符串ID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                字符串名称= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));                如果(的Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))大于0){                    光标pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            +=?,新的String [] {ID},NULL);                     而(pCur.moveToNext()){
                         串phoneNumber的= pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Log.i(TAGphoneNumber的=+ phoneNumber的); // Dupblicate数字印刷
                     }
                }
            }
        }


解决方案

使用设置界面添加电话号码,以避免重复。

Interface.Set的唯一身份=新的HashSet();

检查这个简单的例子

 公共静态无效的主要(字串[] args){
    SET<串GT;唯一身份=新的HashSet<串GT;();
    SET<串GT; DUP的=新的HashSet<串GT;();    对于(字符串一:参数)
        如果(!uniques.add(a))的
            dups.add(一);    //破坏性集差
    uniques.removeAll(DUP的);    的System.out.println(唯一的话:+不重复);
    的System.out.println(重复的话:+的DUP);
  }

从这个链接...

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.

Interface.Set uniques = new HashSet();

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

这篇关于如何从Android中我接触的应用程序中获取独特的联系方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:36