Android获取手机联系人并删除重复项 [英] Android get phone contacts and remove duplicates
问题描述
我遇到与联系人有关的问题。我收到了手机联系人并将其存储在我的列表对象中。这是它的代码
I am having an issue related to contacts. I got the phone contacts and stored them in my list object. Here's the code for it
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = {
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_ID,
ContactsContract.Data.DATA1
};
Cursor phones = getContentResolver().query(
uri, projection, ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.DATA1 + "!=''", null, null);
if (phones.moveToFirst()) {
do {
long ID = phones.getLong(phones.getColumnIndex(projection[0]));
String DisplayName = phones.getString(phones.getColumnIndex(projection[1]));
String photoID = phones.getString(phones.getColumnIndex(projection[2]));
String Key = phones.getString(phones.getColumnIndex(projection[3]));
String photoURI = "null";
if(Key != null && Key.toString().trim().length() > 0 && (Key.startsWith("0") || Key.startsWith("+"))){
if (photoID != null) {
photoURI=String.valueOf(ID);;
//Console.WriteLine("*************************************> id="+ID+" uri="+photoURI.ToString());
}
ContactBean contactModel=new ContactBean(DisplayName,Key,photoID);
list.add(contactModel);
} else {
// No number!!
}
} while (phones.moveToNext());
}
我按照我的要求删除所有联系人和电子邮件联系人。我的问题是我得到所有的联系人,包括重复的联系人。如果我有一个联系人保存3次同名和号码,它将获得所有三个联系人。我不想要这个。有没有办法避免这种情况。 getContactResolver查询中的任何内容,或者我必须删除列表的重复项。任何解决方案或建议?
I am getting all the contacts and email contacts are removed as per my requirements. My issue is i am getting all the contacts including duplicate ones. If i have a contact saved 3 times with same name and number it is getting all the three contacts. I do not want this. Is there any way to avoid this. Anything in getContactResolver query or I have to remove duplicates for my list. Any solutions or suggestions?
推荐答案
你可以试试这个:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
String lastnumber = "0";
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String number = null;
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, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]
{ id }, null);
while (pCur.moveToNext())
{
number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("lastnumber ", lastnumber);
Log.e("number", number);
if (number.equals(lastnumber))
{
}
else
{
lastnumber = number;
Log.e("lastnumber ", lastnumber);
int type = pCur.getInt(pCur.getColumnIndex(Phone.TYPE));
switch (type)
{
case Phone.TYPE_HOME:
Log.e("Not Inserted", "Not inserted");
break;
case Phone.TYPE_MOBILE:
databaseHandler.insertContact(id, name, lastnumber, 0);
break;
case Phone.TYPE_WORK:
Log.e("Not Inserted", "Not inserted");
break;
}
}
}
pCur.close();
}
}
}
这里我先在sqlite数据库中插入数据,然后按名称编写选择查询。
Here i have inserted data in sqlite database first and then Write select query with group by name.
希望有帮助
这篇关于Android获取手机联系人并删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!