Java 在Arraylist中获取应具有联系人的副本
我试图将用户联系人保存在Java 在Arraylist中获取应具有联系人的副本,java,android,android-contacts,phone-number,Java,Android,Android Contacts,Phone Number,我试图将用户联系人保存在ArrayList中,但不知何故,我得到了重复的联系人。我发现我应该使用 ContractsContact.Contacts.CONTENT_URI 而不是 ContractsContact.DATA.CONTENT_URI 但这对我没有帮助。我的ArrayList中仍然有副本。这是我的密码: Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; String _ID = ContactsContract.C
ArrayList
中,但不知何故,我得到了重复的联系人。我发现我应该使用
ContractsContact.Contacts.CONTENT_URI
而不是
ContractsContact.DATA.CONTENT_URI
但这对我没有帮助。我的ArrayList
中仍然有副本。这是我的密码:
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
// Loop for every contact in the phone
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
if (hasPhoneNumber > 0) {
// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);
while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
if (phoneNumber.charAt(0) == '0' && phoneNumber.charAt(1) == '0')
{
String temp = "+";
for(int j=2;j<phoneNumber.length();++j)
temp += phoneNumber.charAt(j);
phoneNumber = temp;
}
if (!phoneNumber.contains("+"))
{
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber normalized_number = null;
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String current_iso_code = telephonyManager.getSimCountryIso();
current_iso_code = current_iso_code.toUpperCase();
try {
normalized_number = phoneUtil.parse(phoneNumber,current_iso_code);
}catch(Exception e){
e.printStackTrace();
}
String str_normalized_number = phoneUtil.format(normalized_number, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
str_normalized_number = str_normalized_number.replaceAll(" ","");
str_normalized_number = str_normalized_number.replaceAll("\\(","");
str_normalized_number = str_normalized_number.replaceAll("\\)","");
contacts.add(new Contact (name,str_normalized_number,current_iso_code,contact_id));
}
else {
phoneNumber = phoneNumber.replaceAll(" ","");
phoneNumber = phoneNumber.replaceAll("\\(","");
phoneNumber = phoneNumber.replaceAll("\\)","");
phoneNumber = phoneNumber.replaceAll("-","");
String iso_code = null;
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
Phonenumber.PhoneNumber numberProto = phoneUtil.parse(phoneNumber, "");
iso_code = phoneUtil.getRegionCodeForNumber(numberProto);
}catch(Exception e){}
contacts.add(new Contact (name,phoneNumber,iso_code,contact_id));
}
}
phoneCursor.close();
}
}
uricontent\u Uri=ContactsContract.Contacts.CONTENT\u Uri;
String _ID=Contacts contact.Contacts.\u ID;
String DISPLAY\u NAME=Contacts contract.Contacts.DISPLAY\u NAME;
字符串HAS\u PHONE\u NUMBER=Contacts contract.Contacts.HAS\u PHONE\u NUMBER;
Uri PhoneCONTENT\u Uri=ContactsContract.CommonDataTypes.Phone.CONTENT\u Uri;
字符串Phone\u CONTACT\u ID=contacts contract.commondatatypes.Phone.CONTACT\u ID;
字符串编号=contacts contract.commonDataTypes.Phone.NUMBER;
ContentResolver ContentResolver=context.getContentResolver();
Cursor Cursor=contentResolver.query(CONTENT\u URI,null,null,null);
//为手机中的每个联系人循环
if(cursor.getCount()>0){
while(cursor.moveToNext()){
String contact_id=cursor.getString(cursor.getColumnIndex(_id));
字符串名称=cursor.getString(cursor.getColumnIndex(DISPLAY_name));
int hasPhoneNumber=Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER));
如果(hasPhoneNumber>0){
//查询并循环联系人的每个电话号码
游标phoneCursor=contentResolver.query(PhoneCONTENT\u URI,null,Phone\u CONTACT\u ID+“=?”,新字符串[]{CONTACT\u ID},null);
while(phoneCursor.moveToNext()){
phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
if(phoneNumber.charAt(0)='0'&&phoneNumber.charAt(1)='0')
{
字符串temp=“+”;
对于(int j=2;j我认为Java中的哈希集概念可能有助于解决这个问题,因为它删除了重复项。我希望它能解决这个问题
下面的链接可能在这方面有所帮助
也许这是一个想法,但它仍然不是一个有效的想法,对吗?我的循环中肯定有问题。如果我找到一个不需要比实际步骤更多的解决方案,速度会快得多