我有一个listview
,我打电话给registerForContextMenu
。在ContextMenu
我只有一个菜单是“删除联系人”。以下是单击删除菜单时的代码:
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case DELETE_ID:
AdapterView.AdapterContextMenuInfo menuinfo;
menuinfo = (AdapterContextMenuInfo)item.getMenuInfo();
int id = menuinfo.position;
delete(id);
return true;
}
return super.onContextItemSelected(item);
}
private void delete(int id)
{
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, ContactsContract.Contacts._ID + "=" + id, 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.Contacts._ID + "=" + id, null);
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}
}
但是使用上面的代码并没有删除任何细节数据。我的问题是上述代码是否丢失或不正确?