質問

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top