我正在制作联系申请,但当我获得所有联系电话时,我得到了重复的电话号码。我如何才能确保只获得唯一编号?
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
接口(interface)添加电话号码,避免重复。
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
关于android - 如何从我在 android 中的联系人应用程序中获取唯一的联系电话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22507960/