我已经尝试过这段代码,但是当我单击删除菜单 ContextItemSelected 时,列表视图中没有丢失任何数据
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case DELETE_ID:
AdapterView.AdapterContextMenuInfo menuinfo;
menuinfo = (AdapterContextMenuInfo)item.getMenuInfo();
int id = menuinfo.position;
deleteContact(id);
populateContactList();
return true;
}
return super.onContextItemSelected(item);
}
public static boolean deleteContact(int id) {
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
Cursor cur = ctx.getContentResolver().query(contactUri, null, ContactsContract.Contacts._ID + "=" + id, null, null);
try {
if (cur.moveToFirst()) {
do {
if (cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)) {
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
ctx.getContentResolver().delete(uri,ContactsContract.Contacts._ID + "=" + id, null);
return true;
}
} while (cur.moveToNext());
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return false;
}
还有其他方法可以删除联系人吗?