我有一个应用程序,通过我的Web服务器上的XML文件将联系人插入手机。该应用程序每天重新插入一次联系人,所以我有最新的联系人和数字等。但是,我想删除第一个插入的所有联系人,所以没有重复。
在插入列表之前,我尝试过的所有代码都会导致所有联系人被删除。因此,这将删除不属于XML列表的其他联系人。
for (int temp = 0; temp < mainList.getLength(); temp++) {
Node mainNode = mainList.item(temp);
Element eElement = (Element) mainNode;
list.add(getChildElementContent(eElement, "NAME") + ": " +
getChildElementContent(eElement, "NUMBER"));
System.out.println(list.get(temp));
ArrayList<ContentProviderOperation> ops = new
ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
while (cur.moveToNext()) {
try{
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
System.out.println("The uri is " + uri.toString());
cr.delete(uri, ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + "=" + getChildElementContent(eElement,"NAME"), null);
System.out.println(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)));
System.out.println( getChildElementContent(eElement,"NAME"));
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}
where子句应该只删除与我的XML文件中的联系人同名的联系人。但它删除了手机上的每个联系人。
try this :
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数据库中插入数据,然后按名称分组编写选择查询。
我非常同意评论,您应该更新您的联系人,而不是删除所有并再次插入它们。
但是,如果您还在这样做,则需要确保在应用程序自己的ACCOUNT_NAME
/ ACCOUNT_TYPE
下插入所有应用程序的联系人。然后,您可以轻松删除仅来自您的应用程序的RawContact
s。
您的代码存在的问题是您要删除整个联系人,而应该只删除RawContacts。
看到你应该如何插入你的应用程序的RawContacts:https://developer.android.com/reference/android/provider/ContactsContract.RawContacts#operations你需要为ACCOUNT_TYPE
找到一个唯一的字符串,这样就不会与其他应用程序发生冲突。
删除所有应用程序的RawContacts
然后很容易通过:
String selection = RawContacts.ACCOUNT_TYPE + "=" + myAccountType;
getContentResolver().delete(RawContacts.CONTENT_URI, selection, null);